We help IT Professionals succeed at work.

clean up text with preg replace

XK8ER
XK8ER asked
on
hello,
I have a little code that its working perfectly fine but I would like to add a little feature so it an also remove bird.org
right now it removes cats.net maybe because it doesn't have a comma right next to it..
<?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';

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

echo $result;
?>

Open in new window

Comment
Watch Question

SILVER EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Change the last space in the pattern to:

[ ,]

Open in new window


so:

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

Open in new window

Author

Commented:
that works nicely..

Explore More ContentExplore courses, solutions, and other research materials related to this topic.