regex - php regular expression to replace HREF attribute -
how use php preg_replace
, regular expression
remove hyperlink contain <a href="#
. here write, doesn't work
$newlink = preg_replace('/^<a href="#(.*)" (?:.*?)>(.*)<\/a>/is', '', $link);
i want replace these links aa anchor mark
<a href="#part1">go part1</a> <a href="#part2">go part2</a> <a href="#part3">go part3</a>
to empty value.
let me start saying using regular expressions parsing/modifying html documents can wrong approach. i'd encourage check out dom document if you're doing other modifications.
with said, making expression non-greedy (.*?
) work.
$newlink = preg_replace('/^<a href="#(.*?)"[^>]+>(.*?)<\/a>/', '', $link);
note: assumes href
first attribute in anchor tags. may poor assumption.
Comments
Post a Comment