XK8ER
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
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;
?>
ASKER
im talking about the cows.net on line 6
Yes, my solution assumes that.
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
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
ASKER
im not getting the same results.. maybe because I am using PHP v5.3.8?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Works for me... note you had cows\.com rather than cows\.net in your pattern.