James
James

Reputation: 876

Error escaping quotes in regular expressions in PHP

I am new to PHP and trying to replace a URL pattern with google.com in the code below.

    $textStr = "Test string contains http://foo.com/more_(than)_one_(parens)
http://foo.com/blah_(wikipedia)#cite-1
http://foo.com/blah_(wikipedia)_blah#cite-1
http://foo.com/unicode_(?)_in_parens
http://foo.com/(something)?after=parens
more urls foo.ca/me some other text";

$pattern = '(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)((?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))*)';

$textStr = preg_replace($pattern, "google.com", $textStr); 

echo $textStr;

I found the regular expression pattern at http://daringfireball.net/2010/07/improved_regex_for_matching_urls but I have not been able to escape the single quote, double quotes in the pattern successfully.

Currently I get the message -- Warning: preg_replace() Unknown modifier '\' But I used the slash() to escape the single quote in {};:\'"

Can someone please help me with the code above?

Upvotes: 0

Views: 909

Answers (2)

mathematical.coffee
mathematical.coffee

Reputation: 56905

In the first place for preg_replace you have to delimit your regular expression by /, as in:

/\b((?:https: ... etc etc)/

Second, since you delimit your regular expressions with / you have to escape any / with a backslash. So https:// -> https:\/\/.

Third, your modifiers (?i) go after the trailing slash:

`/\b((?:https: .. etc etc)/i`

Try (changes made: escaped /, moved regex from (?i)regex to /regex/i):

$pattern = '/\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)((?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))*)/i';
$textStr = preg_replace($pattern, "google.com", $textStr); 

echo $textStr;

Now, since $pattern matches the entire URL you will just get out:

"Test string contains google.com
google.com
google.com
google.com
google.com
more urls google.com some other text"

so all in all, I recommend either @Ampere's answer (but this has a looser regex than your original), or using capturing brackets and backreferences to do something like preg_replace($pattern,'google.com/\2',$textStr) (but modify your capturing brackets appropriately, as this will not work with your current capturing bracket arrangement).

This site is useful for testing things out.

Upvotes: 1

Manigandan Arjunan
Manigandan Arjunan

Reputation: 2265

$patterrn='/([wW]{3,3}\.|)[A-Za-z0-9]+?\./';
$text="Test string contains http://foo.com/more_(than)_one_(parens)
http://foo.com/blah_(wikipedia)#cite-1
http://foo.com/blah_(wikipedia)_blah#cite-1
http://foo.com/unicode_(?)_in_parens
http://foo.com/(something)?after=parens
more urls foo.ca/me some other text";
$output = preg_replace($patterrn,"abc.",$text);
print_r($output);

the output will be ,

Test string contains http://abc.com/more_(than)_one_(parens) http://abc.com/blah_(wikipedia)#cite-1 http://abc.com/blah_(wikipedia)_blah#cite-1 http://abc.com/unicode_(?)_in_parens http://abc.com/(something)?after=parens more urls abc.ca/me some other text

Upvotes: 1

Related Questions