PHP array question

Soldato
Joined
2 Dec 2005
Posts
5,514
Location
Herts
Simple array question. I want to have a page of links. This page reads an external file to get the links, creates an array, then prints them in a certain fashion. The aim of this is so links can be added as simple text, quickly and easily into the external page without knowing any code.

For example, the external page may look like
Code:
http://www.firstlink.com, A link somewhere
http://www.second.com, The second link
and the php somehow manages to echo
Code:
<a href="http://www.firstlink.com">A link somewhere</a><br />
<a href="http://www.second.com">The second link</a><br />

How? :)
 
Code:
<?php

$array = array(
  array('link' => 'http://www.blah.com', 'label' => 'blah'),
  // etc
);

foreach ($array as $a)
{
    echo "<a href=\"{$a['link']}\">{$a['label']}</a>";
}

?>

remembe to use htmlentities() of course.
 
file() to get the file into an array, then a bit of string manipulation to generate the HTML. Not a lot more to it than that. :)
 
  1. Use file() to read each line of the file into an array.
  2. Loop through the array (foreach).
  3. On each iteration, split the current value using the comma as the character to split the string value (explode()).
  4. echo out the resulting two parts, formatting as required
 
Cheers guys. Bit cryptic, but ended up with this, which works:
Code:
<?php
$thelinks = file('thelinks.php');
foreach ($thelinks as $a)
{
	$links = explode(", ",$a);
	echo "<a href=\"{$links[0]}\">{$links[1]}</a><br />";
}
?>
Where does htmlentities come into this? Doesn't seem necessary. Cheers.
 
Last edited:
joeyjojo said:
Cheers guys. Bit cryptic, but ended up with this, which works:
Code:
<?php
$thelinks = file('thelinks.php');
foreach ($thelinks as $a)
{
	$links = explode(", ",$a);
	echo "<a href=\"{$links[0]}\">{$links[1]}</a><br />";
}
?>
Where does htmlentities come into this? Doesn't seem necessary. Cheers.

Does it still work if someone misses the space between the comma and the address by accident? ie http://www.firstlink.com,A link somewhere

Wouldn't it be better just to explode it on the just the comma and then trim the name?
 
For future reference, it's perfectly valid to quote HTML attributes with single quotes—always looks nicer in double-quoted strings than escaped double quotes, IMO.

Compare:

Code:
echo "<a href='$foo'>$bar</a>\n";

to:

Code:
echo "<a href=\"$foo\">$bar</a>\n";

and the even worse:

Code:
echo '<a href="' . $foo .'">' . $bar . "</a>\n";
 
I agree rob.

paulsheffII - fair point. Should be ok here though, just a simple thing to make it easier for the client to stick up his own links.
 
rather than htmlentities, which will change your angular brackets to disable your anchor tags, couldn't you just str_replace("&","&amp;",$file) ?
 
I use the 'even worse' :o

But then most of the time that's because I'm dealing with arrays, which I've found don't always work so well inside quoted strings. Having seen the above code, I now know why.

Won't do it again. :)
 
Seem to be spending a lot of time in HG&P 'Serker, you not got much else on? No forums to moderate...?

:p

(I can derail my thread if I want to :D )
 
Berserker said:
I use the 'even worse' :o

But then most of the time that's because I'm dealing with arrays, which I've found don't always work so well inside quoted strings. Having seen the above code, I now know why.

Won't do it again. :)

Yep, use curly braces for arrays inside double-quoted strings. Object variables work without curly braces, but I typically use them for clarity, e.g.:

Code:
echo "You are logged in as <strong>{$user->username}</strong>.";

You also need to use curly braces where there is ambiguity over a variable's ending, e.g.:

Code:
echo "{$foo}bar";
 
OK - thanks. Hopefully it'll work with my wonderful string indexed and variable indexed arrays as well (big ol' 16GB database-backed website :eek: ).
 
Back
Top Bottom