开发者

php - preg_replace all maito: tags with the email address

开发者 https://www.devze.com 2023-03-27 20:52 出处:网络
similar to this question: preg_match to extract mailto on anchor but I am trying to do a global string replace in php that will convert:

similar to this question:

preg_match to extract mailto on anchor

but I am trying to do a global string replace in php that will convert:

..href="mailto:jeff@mycom.com">Mailme<... (including the "a" tags)

to onl开发者_如何学JAVAy "jeff@mycom.com" with no tags. This needs to be a replace, not an extract.

I have been using preg_replace, but like so many others, am rather poor at regex. It's the regex I'm really after, but best practice is welcome as long as the final solution is clear.

Thanks!


<?php 

$html = '<a href="mailto:jeff@mycom.com">Mailme</a> (including the "a" tags)';
$html = preg_replace("~<a .*?href=[\'|\"]mailto:(.*?)[\'|\"].*?>.*?</a>~", "$1", $html);
echo $html;

working demo


following regex will do work for you.

 preg_replace(/<a .*?href=((?:\'|\"))mailto:(.*?)$1[^>]*>[^<]+</a>/i, "$2", $html);

use [^<]+ between start and end tag because some it may happened that line breaks occur in html, so .* will fail there, [^<]+ this fail in rare case when (less than sign) < appears in tag content. Used flag "i" for in-case sensitivity replace.

0

精彩评论

暂无评论...
验证码 换一张
取 消