Reputation: 47
Me and several friends taking the same programming course have been confused by this for hours so far, hopefully someone here can help. The aim is to take a list of urls, split by new lines, prepend [ img] and append [/ img] for each URL, as suitable for a bulletin board. The actual code includes a switch to allow for both [ img] and [ thumb] bbcodes, but both have the same effect. Instead of outputting
[ img]1[/ img]
[ img]2[/ img]
it outputs
[ img]1
2[ /img]
The same happens for any number of URLs. Here's the code I am using.
<?php
$url_f = (isset($_POST['text'])) ? $_POST['text'] : false;
$thumb = (isset($_POST['type'])) ? $_POST['type'] : false;
$urls = ($url_f) ? explode('\n',$url_f) : '';
?>
<textarea rows='20' cols='40' readonly='1'>
<?php
switch ($thumb){
case 'img':
for ($i = count($urls)-1; $i >= 0; $i--)
{
echo "[img]". $urls[$i] ."[/img]\n";
}
break;
default:
break;
case 'thumb':
for ($i = count($urls)-1; $i >= 0; $i--)
{
echo '[thumb]'. $urls[$i] ."[/thumb]\n";
}
break;
}
?>
</textarea>
Upvotes: 1
Views: 73
Reputation: 13557
Your Problem is '\n' !== "\n"
. The former is treated as "backslash n", while the latter is processed to the line feed character (ASCII 0xA, bein LF). See Double Quoted Strings for more info.
Regarding your loop, you might want to look into foreach.
Upvotes: 1
Reputation: 1324
There are three different types of newlines: \r, \n, and \r\n. I don't do much web development, but different OSes will still probably send different newlines, so you'll have to do some checking to find out what character(s) to split with.
In your code, since \n isn't working, the newline is probably \r\n or \r.
Edit: The single quoted string literal may be the problem.
Upvotes: 1