0

code:

function updateGUI()
    {
        foreach ($GLOBALS['content_array'] as $i)
        {
            $title = $i[0];
            $rating = $i[1];
            echo "<li class=contentList><a class=contentListLink href=filmer_php.php?title=".$title."><span class=spantitle>".$title."</span><span class=spanrating>".asteriskifyRating($rating)."</span></a></li>";
        }
    }

output: enter image description here enter image description here

resources: http://dist3.webbintro.se/assignments/3/movies.txt

context: So I'm creating <li> with <a> attached to them and I'm getting the $title of the <li> from a global variable (not important). The problem is that the query string doesn't contain the full string as some $title contain spaces, so they get cut off at the spaces. I've tried using escape characters \" but didn't work and I can't find any good article about how to properly parse.. Also I would prefer if the solution didn't include regex, but if this can only be solved with regex then so be it.

EDIT1: context: Ok, urlencode fixed the biggest issue. I'm still getting a mojibake square before "Star Wars", any suggestion on what's causing this?

full code: http://jsfiddle.net/Uv5bV/

EDIT2: removed the utf-8 BOM, got the solution from this thread: How to remove multiple UTF-8 BOM sequences before "<!DOCTYPE>"?

code:

function remove_utf8_bom($text)
    {
    $bom = pack('H*','EFBBBF');
    $text = preg_replace("/^$bom/", '', $text);
    return $text;
    }
Community
  • 1
  • 1
Petrus K.
  • 840
  • 7
  • 27
  • 56

1 Answers1

5

Add url encoding:

echo '<li class="contentList"><a class="contentListLink" href="filmer_php.php?title='.urlencode($title).'"><span class="spantitle">'.$title.'</span><span class="spanrating">'.asteriskifyRating($rating).'</span></a></li>';

You should also pay more attention to quotes (single and double).

Lorenzo Marcon
  • 8,029
  • 5
  • 38
  • 63
  • Worked, thanks. Not sure why I didn't search the php reference for this.. The mojibake square before "Star Wars" persists though, do you know how I can remove it? – Petrus K. Nov 19 '13 at 13:44
  • For that you have to check your data before the cycle.. it seems that the weird character it's already present before, because the cycle code looks fine. – Lorenzo Marcon Nov 19 '13 at 13:47
  • 1
    Ok, I've added the code to the question (check the jsfiddle). I removed Star Wars from the textfile and loaded in the 3 entries after that, the mojibake appears before Fight Club now so it seems to be something with the reading process. – Petrus K. Nov 19 '13 at 13:54