1

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>
4
  • 4
    default should come last you know, otherwise it will skip anything beyond it. Commented Oct 25, 2011 at 20:04
  • Doesn't appear to in this case, but thanks for the tip. Commented Oct 25, 2011 at 20:05
  • That switch is really funky. Also have personally never seen the default on anything but the last line of the switch {}. Is $urls actually being populated as expected? Commented Oct 25, 2011 at 20:07
  • Yes, I've tried echoing out $urls[0], $urls[1], etc, they all work fine. Commented Oct 25, 2011 at 20:09

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

That makes sense. How would I go about checking? Could I just explode it, then check the length?
I missed something since I haven't touched PHP in a while. The single quotes you used should be replaced with double quotes in order for the escape sequences to work. Try that, and, if there's still a problem, I'll post code.
Aaaaah! Jeez, reminds me why I hate PHP so much. Spending hours pondering over the difference between single and double quotes...That fixed it. Thanks a lot to everyone who helped.
@user1013452: it's not just a PHP-ism. Many (most? all?) scripting languages that use $ for variables differentiate between ' and " strings.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.