gskartwii
gskartwii

Reputation: 389

preg_replace always gives link to the same page as $1

I am using preg_replace in PHP. When i use this code line:

echo nl2br(preg_replace("(http://.+) ", "<a href='$1'>[link]</a>", $row['description']));

It echoes "<a href=''>[link]</a>". Here is the "description": http://www.youtube.com/watch?v=JDrnz8ZZtUE
What is wrong with this? (I also included the space in the description)

Upvotes: 0

Views: 54

Answers (2)

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

That's because you didn't specify delimiters. PHP will assume the () to be delimiters, thus eliminating your capture group.

"~(http://.+)~"

Upvotes: 4

Kai23
Kai23

Reputation: 1576

You don't have any delimiter. Here is the syntax :

$pattern = '/(\w+) (\d+), (\d+)/i';

Try to do like this :

echo nl2br(preg_replace("/(http://.+)/", "<a href='$1'>[link]</a>", $row['description']));

Upvotes: 0

Related Questions