Link to home
Create AccountLog in
Avatar of XK8ER
XK8ERFlag for United States of America

asked on

preg replace with newline

hello,
I have a little code that its working perfectly fine but I would like to add a little feature so it remove cows.net at the bottom
its not removing cows.net because it doesn't have any text at the end
<?php
$str = 'text here google.com text here
text here http://cows.net/search/query?123 text here
text here cats.net text here
text here bird.org, text here
text here cows.net
text here';

$result = preg_replace('/(?:cows\.com|cats\.net|bird\.org)[, .]/i', '', $str);

echo $result;
?>

Open in new window

Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

$result = preg_replace('/(?:cows\.net|cats\.net|bird\.org)([, .]|$)/im', ' ', $str);

Works for me... note you had cows\.com rather than cows\.net in your pattern.
Avatar of XK8ER

ASKER

im talking about the cows.net on line 6
Yes, my solution assumes that.
Avatar of XK8ER

ASKER

this is what im using

text here google.com text here
text here http://cows.net/search/query?123 text here
text here cats.net text here
text here bird.org, text here
text here cows.net
text here

after your code this is what I get

text here google.com text here
text here http://cows.net/search/query?123 text here
text here text here
text here  text here
text here cows.net
text here

it should remove the last cows.net
My solution worked for me:
<?php
$str = 'text here google.com text here
text here http://cows.net/search/query?123 text here
text here cats.net text here
text here bird.org, text here
text here cows.net
text here';

$result = preg_replace('/(?:cows\.net|cats\.net|bird\.org)([, .]|$)/im', ' ', $str);

echo $result."\n";
?>

Output:
text here google.com text here
text here http://cows.net/search/query?123 text here
text here  text here
text here   text here
text here
text here

Open in new window

Avatar of XK8ER

ASKER

im not getting the same results.. maybe because I am using PHP v5.3.8?
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account