Letter numeration ('A','B','C', etc.) from digit in PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
What is the PHP function to get letter (1 char symbol), based on it position.
Like 0 position - a, 1st - b, etc.
I tried this:
"a"+5
I was expecting f but I get 5 instead.

I think you want to use chr() and/or ord() functions. Something like:
echo chr( ord("a") + $i );

$alphabet = range('a', 'z');
echo array_search('b', $alphabet); // 1

Try this chr() and/or ord() functions.
Some like:
print chr( ord("x") + $z );

Related

count amount of numbers in a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a array of random generated strings looking like this:
63njpn5u
byrtg1za
ht6wnz39
em1yyrju
2ytoxfxl
n5kaho14
zg92pg4n
gr9e7i01
u3t07ai4
I need a php code that will cycle trough these and output the ones back that contain one number. So in the example above I would've gotten back:
2ytoxfxl
em1yyrju
byrtg1za
I've tried using preg_match_all but I cant figure out how to use this with a array.
What you need is preg_grep:
print_r(preg_grep('/^\D*\d\D*$/', $your_array));
You can count like using string method :
$string = "2ytoxfx3l";
echo $int = strlen(intval(preg_replace('/[^0-9]+/', '', $string), 10));
Group of element:
$string = array( "63njpn5u","byrtg1za","ht6wnz39");
foreach($string as $str){
echo strlen(intval(preg_replace('/[^0-9]+/', '', $str), 10));
}

PHP number: decimal point visible only decimal point [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
PHP number: decimal point visible only if needed
This Example using i want to 105.54 "-" Rremove
$foo = "-105.5457";
echo number_format((float)$foo, 2, '.', '');
want to 105.54
not clear enough...
echo number_format(abs((float)$foo), 2, '.', '');
?
Try use abs() "Returns the absolute value of number. " :)
Try this
$foo = "-105.5457";
echo number_format(str_replace('-','',$foo),2,'.','');

PHP: Replace php code in string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want replace php codes in string and running fro server
$x = 1;
$string = "OK:{first}Yes{second}No";
I want replace if($x == 1){ to {first} and } else { to {second}
After run and echo $string , I want result in html :
OK:Yes
How did you come up with this approach? Why not simply:
$string = $x == 1 ? 'OK:Yes' : 'OK:No';
That would get you the string you want based on the value of $x.
Also if you literally mean that you want to do a search-n-replace on the PHP code itself (as #OllyTenerife assumes), then are you going to write it into a file to be executed later, or use eval() or something? Doesn't sound like the right track there... In which case, remodel your code.
$string = str_replace("{first}","if($x == 1)",$string);
$string = str_replace("{second}","} else {",$string);

Split Float & Replace Integer PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have float values in an array... Let's say one of my values is:
5.1234
How do I SWAP the integer in the float. So in the example above, I'd like to swap the 5 with 8. Therefore the new number would be:
8.1234
This needs to be a SWAP, not a mathematical addition as in 5.1234 + 3.
I basically need to split the number in two, the integer (5) and the float value following it (.1234), swap the 5 for the 8 and the recombine them to get 8.1234.
What is the fastest and most elegant way to do this in PHP since I'll be using this on a LOT of data?
To clarify WHY math cannot be used: This is because this is an obj file that's looking for an usemtl library title (Mudbox compliant) from which it extracts the UV space. Then it changes the vert U (or V) accordingly. Problem is these faces may come up more than once. This would make the operation cumulative, which it is NOT. All it needs to do is substituted the integer.
<?php
$number = 5.1234;
$array = explode(".", $number);
// $array[0] contains 5
$newNumber = 8;
$array[0] = $newNumber;
$finalString = $array[0] . '.' . $array[1];
$finalFloat = floatval($finalString); // String to float
echo $finalFloat;
?>
Here is how I would do this. This solution is relevant if you are sure the number will always be formated like followed :
[number].[decimals]
Else you will not be able to always replace the number before the dot.

Create serial number with 8 length digits [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i want to create a format like this: 0000-0000. I already i have the unique number, but i need to merge it with my format type.
$my_id_unique = '60';
//some function....
//IT SHOULD OUTPUT LIKE THIS
$create_serial = '0000-0060';
$x = sprintf("%08d",$my_id_unique);
$create_serial = substr($x,0,4).'-'.substr($x,4);
With oneliner:
$create_serial = implode('-', str_split(sprintf('%08d', $my_id_unique), 4));
Or you could use str_pad:
echo str_pad('60', 9, '0000-0000', STR_PAD_LEFT);

Categories