For example, I have this HTML snippet:
<a href="/sites/all/themes/code.php">some text</a>
The question is - how to cut the text /sites/all/themes/code.php
from the href
with preg_replace()
; which pattern could I use?
For example, I have this HTML snippet:
<a href="/sites/all/themes/code.php">some text</a>
The question is - how to cut the text /sites/all/themes/code.php
from the href
with preg_replace()
; which pattern could I use?
I would strongly recommend against using regular expressions to parse any SGML derivative.
For HTML use some DOM parser. For PHP specifically there is DOMDocument.
you don't have to do a "replace"
(?<=<a href=")[^"]*(?=">)
brings you what you want directly.
test with grep:
kent$ echo '<a href="/sites/all/themes/code.php">some text</a>'|grep -oP '(?<=<a href=")[^"]*(?=">)'
/sites/all/themes/code.php