General Community > Scripting Help
text list to array
Anguz:
if I have a text with urls, one per line, how can they be put into an array?
horus:
I usually use "explode",
explode ( string separator, string string [, int limit]))
checkout php.net [nofollow]
Metho:
This'll go right off the text file, auto put into an array based off of endlines. Very easy. And yes, php.net is your friend.
myurls.txt
----------------
http://www.simplemachines.org
http://www.yabbse.org
http://www.php.net
http://www.google.com
---------------------
--- Code: ---<?PHP
$urlarray = file ('myurls.txt');
echo "$urlarray[2]";
?>
--- End code ---
Will return http://www.php.net. Neato, huh? :D
Methonis
Anguz:
--- Quote ---I usually use "explode",
--- End quote ---
I was thinking about that, but wasn't sure how
--- Quote ---This'll go right off the text file, auto put into an array based off of endlines
--- End quote ---
sweet! I didn't know the file() function!
http://mx2.php.net/manual/en/function.file.php
it says there that the newline is kept at the end of each value of the array... so if I want to use the url in a link, I should trim() it, right? like:
--- Code: ---echo '<a href="' . trim($urlarray['2']) . '">php.net</a>';
--- End code ---
Anguz:
hmm... what if the text is in a variable because it was retrieved from the db? like:
--- Code: ---$urlarray = file($myurls);
--- End code ---
the name of the function doesn't suggest it would work, that's why I ask...
if it doesn't, then, is there a function like this one that'd work with a variable? or would I have to go with explode()?
Navigation
[0] Message Index
[#] Next page
Go to full version