We help IT Professionals succeed at work.

clean text with preg replace

XK8ER
XK8ER asked
on
hello there,
I am trying to use preg replace to clean up a text..
I have posted a sample of what im trying to do here at the bottom.
text here google.com text here
text here http://cows.com/find/ text here
text here http://cats.net/search/query?123 text here
text here http://www.bird.org/hi text here
text here cows.com text here
text here cats.net text here
text here bird.org text here


into

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

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Did you mean to put this in the VB.NET zone!!   ; )


In any event, try:

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

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
I just spotted a rookie mistake. You would probably want to escape the dots in the previous pattern. Although they should still match what you are searching for, they technically match more than just what you are after. For example, the string, "cows com" would match.

Here is the escaped version:

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

Open in new window

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