CMoreira
CMoreira

Reputation: 1708

How to grab http and mailto links in regex

I'm trying to fix a bug on a WP Plugin. It uses this expression to parse the urls:

$anchorPattern = '/\'\"\/\/([^\'\"]+?)\'\">(.*?)</a>/i';

The problem is with the mailto: urls, which are not being matched correctly. So for example, when the code is like:

<a href="mailto:[email protected]" target="_blank"><img src="email.png"></a>

The 2nd match is

mailto:[email protected]" target="_blank"><img src="http:

because the regex only stops at the //. How to I change the regex expression to get the 2nd match to be 'mailto:' ?

regex testing here: http://regex101.com/r/sY4bW8

Upvotes: 0

Views: 326

Answers (2)

Pedro Lobito
Pedro Lobito

Reputation: 98961

Vê se é isso que vc quer:

preg_match('/<a.*?href=["\'](.*?)["\'].*?><img.*?src=["\'](.*?)["\'].*?>/i', $html)

Grupo 1 é o url
Grupo 2 a source da imagem.

http://regex101.com/r/dR5cQ6

Upvotes: 1

sshashank124
sshashank124

Reputation: 32189

You can modify your regex as follows:

<a (.*?)href=[\'\"](.*?:)([^\'\"]+?)[\'\"](.*?)>(.*?)<\/a>

Now it will match mailto:.

Demo

Upvotes: 1

Related Questions