php - How can I separate a string of urls and print each link? -
how can separate each link string:
$field = "www.link1.com www.link2.com";
and output them (expected output):
link1 title www.link1.com link2 title www.link2.com
my current code looks this:
<?php $field = "www.link1.com www.link2.com"; if ($field == "link1"); { $output="link1 title</br>".$field ; } echo $output; ?>
but outputs (current output):
link1 title www.link1.com www.link2.com
so how can change/modify code urls separated , print them shown above?
this should work you:
here first explode()
string array, have each url array element.
then loop through each link , print them. grab name between www.
, next dot preg_replace()
.
$arr = array_map("trim", explode(php_eol, $field)); foreach($arr $v) { echo $v . " title<br>"; echo preg_replace("/^www\.([^\.]*)(.*?)$/", "$1", $v) . "<br><br>"; }
Comments
Post a Comment