tags:

views:

64

answers:

1

Possible Duplicate:
php regular expression help?

hi, i want to replace

i like apple
apple is good
orange is also nice
my mom likes banana
apple 123 4
the pear is soo sweat
my dad loves pear

to

i like apple
apple is good
orange is also nice
my mom likes banana
apple 1
apple 2
apple 3
apple 4
the pear is soo sweat
my dad loves pear

by preg_replace?

+1  A: 
<?
function replace($matches)
{
        return preg_replace("/\s*(\d)\s*/", "$matches[1] $1\n", $matches[2]);
}
$s = "i like apple
apple is good
orange is also nice
my mom likes banana
apple 123 4
the pear is soo sweat
my dad loves pear";
echo "Before::......\n\n$s\n\nAfter::......\n\n";
echo preg_replace_callback('/^(apple) ([\d ]+)\n/m', 'replace', $s);
echo "\n";
?>

Output:

Before::......

i like apple
apple is good
orange is also nice
my mom likes banana
apple 123 4
the pear is soo sweat
my dad loves pear

After::......

i like apple
apple is good
orange is also nice
my mom likes banana
apple 1
apple 2
apple 3
apple 4
the pear is soo sweat
my dad loves pear
Amarghosh
It works!!! I have tried so long time!! thank you so much!!
fukid