Hello everybody,
I need some help with getting a url from href links in a string
example of string:
<?php
$html = "text <a href="http://www.sitename.com">sitename</a> at <a href="http://www.sitename.com">http://www.sitename.com</a>";
?>
How could I change it to make the string become
<?php
$html = "text [http://www.sitename.com]sitname at [http://www.sitename.com]";
?>
Notice the 2nd link. I would not want to display that like twice like so [[url]http://www.sitename.com] http://www.sitename.com[/url]. If the anchor text is the same as the url, I would like to only display the url between brackets.
This is what I tried:
<?php
$urlfind = '/(<a href=")(.*)(">)(.*)(<\/a>)/';
$urlreplace = '[${2}]${4}';
$html = preg_replace($urlfind, $urlreplace, $html);
?>
I appreciate all help.