eloism eloism - 7 days ago 7
PHP Question

merge several strings with a separator in one variable, and seperate them once the variable is called

On my website I need to store several urls on my database.

Rather than storing each of them in a different row I chose to put them all in a single row (since when I call them, I need them all anyways).

To do that I store the urls in a single string separated by

||
like this :


http://url1.com/dhsg.php||http://url2.com/jjipg.php||http://url3.com/dbhm.php||http://url4.com/dheresg.php||...


My question is, once I get this string back in a variable, how can I get back each of theses links in order to put them inside individual span ?
Like :

<span>http://url1.com/...</span>
<span>http://url2.com/...</span>
<span>http://url3.com/...</span>
<span>http://url4.com/...</span>

Answer

What you search is the function explode.

<?php
$yourString = 'http://url1.com/dhsg.php||http://url2.com/jjipg.php||http://url3.com/dbhm.php||http://url4.com/dheresg.php';

$arrayWithURLs = explode('||', $yourString);

echo '<pre>' . print_r($arrWithURLs, TRUE) . '</pre>';
?>

The last echo gives you the result in readable form.

Comments