Pear
Pear

Reputation: 115

PHP preg_replace all except a specific string

In the string:

$text = 'The quick brown fox jumps over the lazy dog';

How can I preg_replace all except "fox"?

Something like:

$new = preg_replace('(.*?)|^[fox]|(.*?)si', '', $text);

Note:
I'm looking for a preg_replace() solution, not explode() or preg_match_all().

Thanks for any help!

Upvotes: 0

Views: 3233

Answers (2)

Toto
Toto

Reputation: 91428

Not I well understand your need, but how about:

$new = preg_replace('/^.*?(fox).*?$/si', '$1', $text);

Upvotes: 1

Federico Piazza
Federico Piazza

Reputation: 31005

You can use this regex to grab the path/whatever as you pointed.

(path\/[^\[\]\s]+)

Working demo*

enter image description here

What I'm not clear about is related to your output string since you put:

"path/file1.txtpath/file2.jpg[/imgpath/file3 to a [i"

My regex would help you to generate:

"path/file1.txtpath/file2.jpgpath/file3"

Can you confirm is above is ok?

Upvotes: 0

Related Questions