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
First of all you have wrong file format and as a result - funny code to read it
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.
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>