web-nomad
web-nomad

Reputation: 6013

Get href value between two substrings

I am trying to capture the following pattern:

<a href="http://cdn.xyz.com/media/info.pdf" target="_blank">

This is what I am trying:

preg_match_all( '/(<[a-zA-Z]+[^>]+>)/ism', $str, $matches);

This is not capturing the above pattern.

How should I restructure the pattern?

Upvotes: 0

Views: 43

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174854

You could to use a negative lookahead assertion based regex.

preg_match_all('~<[a-zA-Z]+(?:(?!&[lg]t;).)*>~isg', $str, $matches);

(?:(?!&[lg]t;).)* matches any character but not of < or >. That is, it checks whether the character going to be matched won't be the starting letter in < or >.

OR

<[a-zA-Z]+.*?>

DEMO

Upvotes: 1

vks
vks

Reputation: 67988

(<[a-zA-Z]+.+?>)

You can use this simple pattern.See demo.

https://regex101.com/r/bW3aR1/14

Your regex contains [^>]+ which is not what you think.It is just a class of characters which should not appear.The order is not defined over here.

Upvotes: 1

Related Questions