Help with generating a simple list of urls

After much trial and error, reading and searching, I can’t seem to work out how to do this. If you can help, your advise & tips will be greatly appreciated!

I have php code that accesses a file that contains a list like this:

url1.com : 1 Linked Text ; Short description of the url1
~
url2.com : 2 Linked Text ; Short description of the url2
~
url3.com : 3 Linked Text ; Short description of the url3

Here is the code

<?php
$list = join (‘’, file (‘list.php’));
$content = split(“~”,$list);
$rows = str_replace(array(’ : ‘,’ ; ‘),’<br>',$content);
#$rows = array_slice($rows, 0, 10); // limits the list
echo “<ol>”;
foreach ($rows as $row)
{
echo “<li>”;
echo ($row); // displays the content without html
// the line below only shows 1 character not the full word
#echo “<a href=”.$row[0].“>”.$row[1]."</a> - ".$row[2];
echo “</li>”;
}
echo “</ol>”;
?>

I am wanting the output to have a row somewhat like this:
<a href=“url1”>1 Linked Text</a> Short description for the url1

Any ideas what I am doing wrong?

First of all you have wrong file format and as a result - funny code to read it :slight_smile:
Line break itself is good record separator. And widely used one. PHP even has a function to read such a format. a file() function.
So, make your file as this:

url1.com : 1 Linked Text ; Short description of the url1
url2.com : 2 Linked Text ; Short description of the url2
url3.com : 3 Linked Text ; Short description of the url3 

name it list.txt, not list.php,
and then use just this code to get it into array:

$content = file ('list.txt'));

Now, note that $content is an array. You can’t use any string function on an array.

$rows is an array of strings. So in the foreach $row is a string.
$row[0] will be the first character, $row[1] the second, $row[2] the third
*in some the character is whitespace i.e. newline

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

$test_str = "url1.com : 1 Linked Text ; Short description of the url1
~
url2.com : 2 Linked Text ; Short description of the url2
~
url3.com : 3 Linked Text ; Short description of the url3";

$content = split("~",$test_str);
$rows = str_replace(array(' : ',' ; '),'<br>',$content);
#$rows = array_slice($rows, 0, 10); // limits the list

var_dump($rows);

echo "<ol>\\r\
";
foreach ($rows as $row)
{
	echo "<li>\\r\
";
	echo ($row); // displays the content without html
	// the line below only shows 1 character not the full word
	echo "<a href=".$row[0].">".$row[1]."</a> - ".$row[2] . "\\r\
";
	echo "</li>\\r\
";
}
echo "</ol>";
?>

It’s easier to see whats going on if you look at the view-source.

Thank you for the help so far. I have implemented the change to a txt file and now simple use the line break not the ~ which works fine.

Since I am wanting the output to have a row somewhat like this:
<a href=“url1”>1 Linked Text</a> Short description for the url1

do I need it to be read into an array or string so that I can use the different parts in different orders per row?
ie. Each row will have 3 parts I can and want to call: the url, the linked text, and the description.

Any others tips or code of how to achieve this?

loop over your array, getting lines one-by-one and split them inside of loop, not before.

Ok I do understand what you sat but do not know how to do that. What goes where?

Also, so am I needing to split the string into an array with 3 areas that then have keys which I can call?

So now I have this code:


<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

$list = join ('', file ('list.txt'));
$content = split("\
",$list);
#$rows = str_replace(array(' : ',' ; '),'<br>',$content);
$rows = $content;
$rows = array_slice($rows, 0, 10); // limits the list
echo "<ol>";
foreach ($rows as $row) 
{ 
#$row = str_replace(array(' : ',' ; '),'<br>',$row);
$row0 = str_replace(' : ','/',$row);
$row1 = str_replace(' : ','<br> row1 ',$row0);
$row2 = str_replace(' ; ','<br> row 2 ',$row1);
echo "<li>";
#echo ($row); // displays the content without html
// the line below only shows 1 character not the full word
echo "<a href=".$row0.">".$row1."</a> - ".$row2;
echo "</li>"; 
} 
echo "</ol>";
?>

how to do what? to move a few lines of code in your script? Use copy and paste feature of your text editor.

$list = join (‘’, file (‘list.txt’));
$content = split("
",$list);

I can’t believe it

<?php
$list = join (‘’, file (‘list.php’));

$content = split(“~”,$list);
$rows = str_replace(array(’ : ‘,’ ; ‘),’:',$content);
echo “<ol>”;
foreach ($rows as $val)
{
$row=split(“:”,$val);
echo “<li>”;
echo “<a href=”.$row[0].“>”.$row[1]."</a> - ".$row[2];
echo “</li>”;
}
echo “</ol>”;
?>

it’s so easy,just do this~~~

Thanks for your help. I must not have explained myself well. I do know how to copy and paste. What I didn’t now was what code to write where.

This is what I now have and it works but only shows 1 entry not the 3 or more.


<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

$list = join ('', file ('list.txt'));

$content = split("/n",$list);
$rows = str_replace(array(' : ',' ; '),':',$content);
echo "<ol>";
foreach ($rows as $val) 
{ 
$row=split(":",$val);
echo "<li>";
echo "<a href=".$row[0].">".$row[1]."</a> - ".$row[2];
echo "</li>"; 
} 
echo "</ol>";
?>

So this is what the html should look like once I get this code correct when it gets the list info from the txt file which has:

url0.com : 0 Linked Text ; Short description of the url0
url1.com : 1 Linked Text ; Short description of the url1
url2.com : 2 Linked Text ; Short description of the url2


<ol>
<li><a href=url0.com>0 Linked Text</a> - Short description of the url0</li>
<li><a href=url1.com>1 Linked Text</a> - Short description of the url1</li>
<li><a href=url2.com>2 Linked Text</a> - Short description of the url2</li>
</ol>

Well, I am being dumb again.
Take my apologies please.
I thought str_replace don’t work with arrays.

As for your code, there is double mistake:

  1. A new line character in your split() is represented by
    sequence, not /n
  2. no split() needed at all. file() already does the job.
    So, make it just
    $content = file (‘list.txt’));
    without join and split

Perfect! This is how it looks:


<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

$content = file ('list.txt');
$rows = str_replace(array(' : ',' ; '),':',$content);
$rows = array_slice($rows, 0, 10); // limits the list
shuffle ($rows); // randomises the rows
echo "<ol>";
foreach ($rows as $val) 
{ 
$row=split(":",$val);
echo "<li>";
echo "<a href=".$row[0].">".$row[1]."</a> - ".$row[2];
echo "</li>"; 
} 
echo "</ol>";
?>

Thank you very much!