Ricky
Ricky

Reputation: 275

preg_replace with URL problem

I use a preg_replace to auto insert HTML links within paragraphs.

Here's what I currently use:

$pattern = "~(?!(?:[^<\[]+[>\]]|[^>\]]+<\/a>))(".preg_quote($find_keyword, '/').")\b~msUi";
$replacement = "<a href=\"http://$kw_url\" title=\"$find_keyword\">\$0</a>"; 
$article_content = preg_replace($pattern, $replacement, stripslashes($article_content), 1, $added );

It works great, except 1 problem:

It doesn't match and replace if the keyword is a URL.

If: $find_keyword="http://www.mysite.com/" it won't come up with any matches even though it's in the content.

I already tried escaping $find_keyword with preg_quote, which didn't make any different.

Any regex experts know a solution? Thanks.

Upvotes: 0

Views: 527

Answers (2)

Christopher Manning
Christopher Manning

Reputation: 4567

You can run your find_keyword through

$find_keyword=preg_quote("http://www.mysite.com/", '/');

http://www.php.net/manual/en/function.preg-quote.php

Upvotes: 0

vicTROLLA
vicTROLLA

Reputation: 1529

The forward slashes in your $find_keywords are not escaped which is breaking the pattern.

Upvotes: 1

Related Questions