Welcome Guest, Not a member yet? Register   Sign In
php preg_replace function
#1

[eluser]basty_dread[/eluser]
Hello,
I need your help guys about this preg replace function

this is the string i need to replace:

Code:
$string = "the quick brown fox jumps over the lazy dog, Brown Fox";
$find = "brown fox";

the output should be

the quick brownOfox jumps over the lazy dog, BrownOFox

can you help me with the right preg replace.. please. Thank you..

Code:
$save = preg_replace('/\b('.$find.')\b/i', <i dont know what to put here>, $string );

many thanks..
#2

[eluser]sophistry[/eluser]
you want to replace space characters inside an arbitrary string?
#3

[eluser]basty_dread[/eluser]
is that possible?
#4

[eluser]sophistry[/eluser]
is your birthday close? :-)
Code:
$string = "the quick brown fox jumps over the lazy dog, Brown Fox";
$find = "brown fox";
$save = preg_replace('/\b('.$find.')\b/ie', "str_replace(' ', 'O', '$1')", $string);
print_r($save);
#5

[eluser]basty_dread[/eluser]
Hi sophistry,
Thanks for helping on my topic preg_replace function
its really a big help..

i need your help again on this..
if you notice that it will surely match the word enclosed in <h2> tag..
is there a way it could exclude it from being replaced?

Code:
$string = "the quick brown fox jumps over the lazy dog, Brown Fox. <h2>The Brown Fox</h2>";
$find = "brown fox";
$save = preg_replace('/b('.$find.')b/ie', "str_replace(' ', 'x', '$1')", $string);
print_r($save);
#6

[eluser]sophistry[/eluser]
yes, but it just involves breaking down your problem into logical parts instead of trying to cram everything into some magical/unreadable regex!

before you run the replace, just strip out any text you want to exclude (putting markers in place so you can re-insert the text later).

cheers.
#7

[eluser]basty_dread[/eluser]
how about to exclude the h1-h9 tag i would like to put - (dash) on every spaces..
Code:
$string = "the quick brown brown fox jumps over the lazy dog, Brown Fox. <h2>The Brown Fox</h2>";
$save = preg_replace('/<h.*>.*<\/h.*>/ie'," <what shouldi put here?&gt;" , $string);

print_r($save);
#8

[eluser]basty_dread[/eluser]
and also to turn it back to the original when the replacement is done?

if you could notice its already replaced and it put - on each spaces..
how could i remove that ?
Code:
$string = "the quick brown brown fox jumps over the lazy dog, Brown Fox. <h2>The-Brown-Fox</h2>";
$save = preg_replace('/<h.*>.*<\/h.*>/ie'," <what shouldi put here?&gt;" , $string);

Thanks..
#9

[eluser]sophistry[/eluser]
try something and post the code? ;-)
#10

[eluser]basty_dread[/eluser]
i tried this but, actually i dont know what is the use of $1,\1?
it outputs empty, but it replaced the word with h2 tag
Code:
$string = "the quick brown brown fox jumps over the lazy dog, Brown Fox. <h2>The Brown Fox</h2>";
$save = preg_replace('/<h.*>.*<\/h.*>/ie',"str_replace('','-','$1')" , $string);
print_r($save);




Theme © iAndrew 2016 - Forum software by © MyBB