I need help to change a character in php.
I got some code from the web:
char dest='a';
int conv=(int)dest;
Can I use this code to convert a character into numeric? Or do you have any ideas?
I just want to show the result as a decimal number:
if null == 0
if A == 1
Use ord() to return the ascii value. Subtract 96 to return a number where a=1, b=2....
Upper and lower case letters have different ASCII values, so if you want to handle them the same, you can use strtolower() to convert upper case to lower case.
To handle the NULL case, simply use if($dest). This will be true if $dest is something other than NULL or 0.
PHP is a loosely typed language, so there is no need to declare the types. So char dest='a'; is incorrect. Variables have $ prefix in PHP and no type declaration, so it should be $dest = 'a';.
Live Example
<?php
function toNumber($dest)
{
if ($dest)
return ord(strtolower($dest)) - 96;
else
return 0;
}
// Let's test the function...
echo toNumber(NULL) . " ";
echo toNumber('a') . " ";
echo toNumber('B') . " ";
echo toNumber('c');
// Output is:
// 0 1 2 3
?>
PS:
You can look at the ASCII values here.
It does indeed work as in the sample, except that you should be using php syntax (and as a sidenote: the language that code you found most probably was, it did not do the same thing).
So:
$in = "123";
$out = (int)$in;
Afterwards the following will be true:
$out === 123
This may help you:
http://www.php.net/manual/en/function.ord.php
So, if you need the ASCII code you will need to do:
$dest = 'a';
$conv = ord($dest);
If you want something like:
a == 1
b == 2
.
.
.
you should do:
$dest = 'a';
$conv = ord($dest)-96;
For more info on the ASCII codes: http://www.asciitable.com/
And for the function ord: http://www.php.net/manual/en/function.ord.php
It's very hard to answer because it's not a real question but just a little bit of it.
But if you ask.
It seems you need some translation table, that defines links between letters and numbers
A -> 2
B -> 3
C -> 4
S -> 1
or whatever.
You can achieve this by using an array, where keys would be these letters and values - desired numbers.
$defects_arr = array(
'A' -> 2,
'B' -> 3,
'C' -> 4'
'S' -> 1
};
Thus, you can convert these letters to numbers
$letter = 'A';
$number = $defects_arr($letter);
echo $number; // outputs 1
But it still seems is not what you want.
Do these defect types have any verbose equivalents? If so, why not to use them instead of letters?
Telling the whole story instead of little bit of it will help you to avoid mistakes and will save a ton of time, both yours and those who to answer.
Out of this question, if you are looking for convert RT0005 to 5
$max = 'RT0005';
return base_convert($max,10,10);
// return 5
Related
Given any character from a to z, what is the most efficient way to get the next letter in the alphabet using PHP?
The most efficient way of doing this in my opinion is to just increment the string variable.
$str = 'a';
echo ++$str; // prints 'b'
$str = 'z';
echo ++$str; // prints 'aa'
As seen incrementing 'z' give 'aa' if you don't want this but instead want to reset to get an 'a' you can simply check the length of the resulting string and if its >1 reset it.
$ch = 'a';
$next_ch = ++$ch;
if (strlen($next_ch) > 1) { // if you go beyond z or Z reset to a or A
$next_ch = $next_ch[0];
}
It depends on what you want to do when you hit Z, but you have a few options:
$nextChar = chr(ord($currChar) + 1); // "a" -> "b", "z" -> "{"
You could also make use of PHP's range() function:
$chars = range('a', 'z'); // ['a', 'b', 'c', 'd', ...]
Well, it depends what exactly you want to do with the "edge cases". What result do you expect when the character is z or Z? Do you want the next letter of the same case, or just the next letter, period?
Without knowing the answer to that, for the very basic case, you can just do this:
$next_character = chr(ord($current_character) + 1);
But when you're at Z this will give you [, and z will give you {, according to ASCII values.
Edited as per comment:
If you need the next character of the same case, you can probably just add simple checks after the line above:
if ($next_character == '[')
$next_character = 'A';
else if ($next_character == '{')
$next_character = 'a';
These are very simple operations, I really wouldn't worry about efficiency in a case like this.
How about using ord() and chr()?
<?php
$next = chr(ord($prev)+1);
?>
Since I only care about lowercase characters in this case, I'll use the following code, based on the answers posted here:
function nextLetter(&$str) {
$str = ('z' === $str ? 'a' : ++$str);
}
Thanks for the help, guys!
$val = 'z';
echo chr((((ord($val) - 97) + 1) % 26) + 97);
Nice and easy :-)
Create an array of all letters, search for existing letter and return its next letter. If you reach the last letter return first letter.
Hi i need to save a 010 number in $number and if i do like this php will remove the starting 0
$number = 010
And echo of this will return 10 how can i make it not to remove the initial 0
BR
Martin
Use it as a String:
$number = '010';
Use str_pad() function.
echo str_pad('10',3,'0',STR_PAD_LEFT)
http://php.net/manual/en/function.str-pad.php
Do remember that numbers starting with 0 can also be treated as octal number notation by the PHP compiler, hence if you want to work with decimal numbers, simply use:
$num = '010';
This way the number is saved, can be stored in the database and manipulated like any other number. (Thx to the fact that PHP is very loosely typed language.)
Another method to use would be:
Save number as $num = 10;
Later while printing the value you can use sprintf, like:
sprintf("%03d", $i);
This will print your number in 3 digit format, hence 0 will be added automatically.
Another method:
<?php
$num = 10;
$zerofill = 3;
echo str_pad($num, $zerofill, "0", STR_PAD_LEFT);
/* Returns the wanted result of '010' */
?>
You can have a look at the various options available to you and make a decision. Each of the method given above will give you a correct output.
how does the "tiny url" sites get so tiny ID url ?
i mean this : blabla.com/JH7
how can i get to such result? a functionality that is like md5 that does not repeat it self.
thanks in advance!
For example you can simply iterate trough string:
php > $str = 'aaa';
php > $str++;
php > echo $str;
aab
The another option is to prepare function which will generate random strings containing of a-zA-Z0-9 and than generate few millions of them into db (so you could just use them when needed) or do it in loop:
while( 1){
$rand = randomString();
if( isUnique( $rand)){
break;
}
}
Make a database table with the columns short_url and url.
Start by inserting the record a, example.com.
Increment short_url with each new entry (b, c, ..., a1 ...).
That's basically how these services work.
They use base36 encoding to convert an integer to a compact string like that.
Using PHP:
<?php
$id = 18367;
$base36 = base_convert($id, 10, 36); // convert to base36 "e67"
$base10 = base_convert($base36, 36, 10); // "e67" back to base 10, $id
As stated by deceze, base62 is also suitable which gives you a character set of a-zA-Z0-9 instead of just a-z0-9 like base36 does.
How do I output a value as a number in php? I suspect I have a php value but it is outputting as text and not as a number.
Thanks
Here is the code - Updated for David from question below
<?php
if (preg_match('/\-(\d+)\.asp$/', $pagename1, $a))
{
$pageNumber = $a[1];}
else
{ // failed to match number from URL}
}
?>
If I call it in: This code it does not seem to work.
$maxRows_rs_datareviews = 10;
$pageNum_rs_datareviews = $pagename1; <<<<<------ This is where I want to use it.
if (isset($_GET['pageNum_rs_datareviews'])) {
$pageNum_rs_datareviews = $_GET['pageNum_rs_datareviews'];
}
If I make page name a static number like 3 the code works, if I use $pagename1 it does not, this gives me the idea $pagename1 is not seen as a number?
My stupidity!!!! - I used $pagename1 instead of pageNumber
What kind of number? An integer, decimal, float, something else?
Probably the easiest method is to use printf(), eg
printf('The number %d is an integer', $number);
printf('The number %0.2f has two decimal places', $number);
This might be blindingly obvious but it looks like you want to use
$pageNum_rs_datareviews = $pageNumber;
and not
$pageNum_rs_datareviews = $pagename1;
echo (int)$number; // integer 123
echo (float)$number; // float 123.45
would be the easiest
I prefer to use number_format:
echo number_format(56.30124355436,2).'%'; // 56.30%
echo number_format(56.30124355436,0).'%'; // 56%
$num = 5;
echo $num;
Any output is text, since it's output. It doesn't matter what the type of what you're outputting is, since the human eye will see it as text. It's how you actually treat is in the code is what matters.
Converting (casting) a string to a number is different. You can do stuff like:
$num = (int) $string;
$num = intval($string);
Googling php string to number should give you a beautiful array of choices.
Edit: To scrape a number from something, you can use preg_match('/\d+/', $string, $number). $number will now contain all numbers in $string.
Given any character from a to z, what is the most efficient way to get the next letter in the alphabet using PHP?
The most efficient way of doing this in my opinion is to just increment the string variable.
$str = 'a';
echo ++$str; // prints 'b'
$str = 'z';
echo ++$str; // prints 'aa'
As seen incrementing 'z' give 'aa' if you don't want this but instead want to reset to get an 'a' you can simply check the length of the resulting string and if its >1 reset it.
$ch = 'a';
$next_ch = ++$ch;
if (strlen($next_ch) > 1) { // if you go beyond z or Z reset to a or A
$next_ch = $next_ch[0];
}
It depends on what you want to do when you hit Z, but you have a few options:
$nextChar = chr(ord($currChar) + 1); // "a" -> "b", "z" -> "{"
You could also make use of PHP's range() function:
$chars = range('a', 'z'); // ['a', 'b', 'c', 'd', ...]
Well, it depends what exactly you want to do with the "edge cases". What result do you expect when the character is z or Z? Do you want the next letter of the same case, or just the next letter, period?
Without knowing the answer to that, for the very basic case, you can just do this:
$next_character = chr(ord($current_character) + 1);
But when you're at Z this will give you [, and z will give you {, according to ASCII values.
Edited as per comment:
If you need the next character of the same case, you can probably just add simple checks after the line above:
if ($next_character == '[')
$next_character = 'A';
else if ($next_character == '{')
$next_character = 'a';
These are very simple operations, I really wouldn't worry about efficiency in a case like this.
How about using ord() and chr()?
<?php
$next = chr(ord($prev)+1);
?>
Since I only care about lowercase characters in this case, I'll use the following code, based on the answers posted here:
function nextLetter(&$str) {
$str = ('z' === $str ? 'a' : ++$str);
}
Thanks for the help, guys!
$val = 'z';
echo chr((((ord($val) - 97) + 1) % 26) + 97);
Nice and easy :-)
Create an array of all letters, search for existing letter and return its next letter. If you reach the last letter return first letter.