Patrick
Patrick

Reputation: 490

Regex for [title](http://) links

I'm trying to parse links using php with a structure such as [google](http://google.com), and so far I've got this:

"/\[(.+?)\]((.+?\))/is" 

The only part I can't get to work is the part with the parenthesis ')'.

Any help would be great!

[edit]

@jari - This is part of the code:

$find = array(
    "@\n@", 
    "/[**](.+?)[**]/is",
    "/\[(.+?)\]\((.+?)\)/is"
    );

$replace = array(
    "<br />",
    "<strong>$1</strong>",
    "<a href\"$1\" target=\"_blank\" rel=\"no follow\"></a>"
    );

$body = htmlspecialchars($body); 
$body = preg_replace($find, $replace, $body); 
return $body; 

Upvotes: 0

Views: 89

Answers (2)

Tom
Tom

Reputation: 16198

It should look something like:

\[([^\]]+)]\([^)]+\)

We are using [^x] which means, match anything that is not x.
We have used this to capture in group one everything after the [ which is not a ]. Same for the second group using [^)]+. Anything that is not a ).

Upvotes: 0

helmbert
helmbert

Reputation: 37994

The parenthesis is a special character and usually marks sub-patterns inside your expression, so you need to escape them (just as you did with the square brackets, btw):

"/\[(.+?)\]\((.+?)\)/is"

Upvotes: 1

Related Questions