Your question does not show any effort, however the following might come useful to you:
It very much depends how long your numbers would be? Assuming 0 to 9, you would do this:
$numbers = array(0,1,2,3,4,5,6,7,8,9);
$number_words = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
$string = "I have 3 apples.";
$new_string = str_replace($numbers, $number_words, $string);
The above solution is for simple words and replacements.
For instance, for numbers such as 1995445 you should then search for functions one the internet (or write one) which would convert numbers to string.
Here is a good function to do this:
http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/
What we do, is first extract the number from the string:
$rule = "/([0-9]+)/";
$string = "I have 2 mobile phones, each containing 2500 messages";
$num_match;
Then we loop through the string. Each time we only replace the first occurred number, capture it, pass it to our number_to_string()
function and then get the string, use that returned string in our replace function which is preg_replace(). We utilize preg_replace()
's $limit
param to limit replacement only to first occurrence on each iteration:
while( preg_match($rule, $string, $num_match) )
{
$string = preg_replace("/".$num_match[0]."/", number_to_string($num_match[0]), $string, 1);
}
echo $string;
What I get in my browser is then:
I have two mobile phones, each containing two thousands and five hundred messages