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

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -