How Can I change a number to string [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there an easy way to convert a number to a word in PHP?
Recently I needed to change a number to string just like below in php
$str=100;
{ Here will be the code which will return One Hundred not 100}
so $str=150 then will return one hundred fifty
How can I do it. How can I change the number to text in php (NB: number will be random)
please help me

Use Numbers_Words Pear package.
Example.
require('Numbers/Words.php');
$number = 150;
$nw = new Numbers_Words();
$numw = $nw->toWords($number);
echo $numw; //one hundred fifty

The solution to this problem has been suggested in the comments of the php documentation at the following location: http://www.php.net/manual/en/function.number-format.php#97020
Hope that helps

There is a PEAR package that will do this for you ...
http://pear.php.net/package-info.php?package=Numbers_Words

Related

How would I take a string, find a certain character, add a character, and continue until end of string? [duplicate]

This question already has answers here:
PHP: Variables in a Postgresql Query
(2 answers)
Closed 3 years ago.
I’m working on a project where I need to use postgresql to update info. I need to take
Martin’s chik ‘n’ chips
And make change it to
Martin\’s chik \’n\’ chips
How would I do this? I’ve looked at other posts, and found out to use substr() to create the new string and strpos() to find the ‘s, and even setting a new variable to keep the position of the previous ‘
Edit: thanks everyone, clearly didn’t do enough research!
If in PHP:
Check out str_replace(). e.g.
$text = "Martin’s chik ‘n’ chips";
$apostrophe = array("'","`","‘","’");
$newtext = str_replace($apostrophe,"\'",$text);
In this specific example, if you don't have any of the 'fancy' apostrophes, check out addslashes() as this will solve everything for you

PHP regex on mention (#name) [duplicate]

This question already has answers here:
Finding #mentions in string
(6 answers)
Closed 5 years ago.
I have busting my balls on this one, and I have been trying numerous regex'es but just can't seem to get it right. (I am not that experienced in regex).
The following situation is going on, lets take this basic sentence for example;
I recently saw #john-doe riding a bike, did you noticed that too #foo-bar?
The trick here is to get only the #john-doe and #foo-bar parts from the string, preferably in an array:
$arr = [
'#john-doe',
'#foo-bar'
];
Could someone help me get on the right track?
You can use this regex (\#(?P<name>[a-zA-Z\-\_]+)) :
<?php
$matches = [];
$text = "I recently saw #john-doe riding a bike, did you noticed that too #foo-bar?";
preg_match_all ("(\#(?P<names>[a-zA-Z\-\_]+))" ,$text, $matches);
var_dump($matches['names']);
In this example, I used the ?P<names> to name the capture groups, it's easier to get it.
I've made a Regex101 for you, and a PHP sandbox for test
https://regex101.com/r/ZFWvCG/1
http://sandbox.onlinephpfunctions.com/code/1d04ce64a2a290994bf0effd7cf8f0039f20277b

php Rounding up numbers (3628 > 3630) [duplicate]

This question already has answers here:
How to round up a number to nearest 10?
(16 answers)
Closed 6 years ago.
After converting currencies I end up with the numbers below:
3628, 5987 and 2359.
I'd like to round them up so they would appear as 3630, 5990 and 2360.
What is the best approach to accomplish this?
My idea of doing this is adding a 0. in front of the number to achieve 0.3628, then using round(0.3628, 3) so that I would get 0.363 and then finally id have to remove 0. and add another zero at the end to achieve 0.3630.
There must be a better way to do it.
Like so:
echo ceil(5987 / 10) * 10;
outputs 5990

Computing limits in PHP [duplicate]

This question already has answers here:
Is there something that's larger than any numbers in PHP?
(9 answers)
Closed 8 years ago.
I want to compute n^(2/3) as n approaches infinity in PHP.
What I tried:
$n = "INF";
echo pow($n, (2/3));
Desired result:
INF
Actual result:
0
Any suggestions? How to compute limits in PHP?
UPDATE:
OK, first problem solved - I just needed to remove the quotation mark.
But is this actually the way to calculate a limit? Is it interpreted as a limit?
You're passing in the string "INF" into the exponential expression, which isn't valid. Try passing in just POW:
$n = INF;
echo pow($n, (2/3));
// INF

convert number from 12 to 12.00 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In PHP, how to print a number with 2 decimals, but only if there are decimals already?
Brothers I want to convert me any way I entered into a text field automatically turns
12 to 12.00
How possible work by php and javascript
Thanks
In PHP its easy to do that by doing:
number_format($numberVar, 2);
Try this number_format() from PHP.net:
echo number_format(12, 2)
What, no javascript?
var x = 3;
alert(x.toFixed(2)); // 3.00
Will "work" for the case given, but so will:
alert(x + '.00');
There are bugs in toFixed in some older browsers with certain values so many write their own simple routine.
$thisnum = $thisnum . ".00";
Proof of concept:
$thisnum = $thisnum . ".00";
echo ($thisnum + 4.55);
However, as pointed out by the community, this is something that will only work for the case mentioned in the question and will not handle any use cases that already have decimals. The other answers are better.

Categories