views:

37

answers:

2
$q = "my phrase with special chars like úção!?";

while ($i < strlen($q)) {

echo '<img src="'.$q[$i].'.png" />';

$i++;
}

i also tried with switch/case but when i have chars like ç Ç ã â i cant get the image replacing to work since its more than one char (i was converting to html entities and then searching in the case ccedil but its not possible)

thank you

+1  A: 

It won't work because accents aint allowed in a file name.

A workaround: Try naming your image file with accents with a supported name. Eg: é => eacute

With this code:

<?php
$q = "my phrase with special chars like úção!?";

while ($i < strlen($q)) {
    if ($q[$i] == " ")
    {
        echo '<img src="space.png" />';
} else if ($q[$i] == "?") {
    echo '<img src="questionmark.png" />';
    } else {
        echo '<img src="'.str_replace(array("&", ";"), "", htmlentities($q[$i])).'.png" />';
    }

    $i++;
}
?>

This code also support space. You need an image named space.png though.

Here is the generated output: <img src="m.png" /><img src="y.png" /><img src="space.png" /><img src="p.png" /><img src="h.png" /><img src="r.png" /><img src="a.png" /><img src="s.png" /><img src="e.png" /><img src="space.png" /><img src="w.png" /><img src="i.png" /><img src="t.png" /><img src="h.png" /><img src="space.png" /><img src="s.png" /><img src="p.png" /><img src="e.png" /><img src="c.png" /><img src="i.png" /><img src="a.png" /><img src="l.png" /><img src="space.png" /><img src="c.png" /><img src="h.png" /><img src="a.png" /><img src="r.png" /><img src="s.png" /><img src="space.png" /><img src="l.png" /><img src="i.png" /><img src="k.png" /><img src="e.png" /><img src="space.png" /><img src="uacute.png" /><img src="ccedil.png" /><img src="atilde.png" /><img src="o.png" /><img src="!.png" /><img src="questionmark.png" />

Cybrix
Well, I dont know if accents aren't supported into file name or simply the HTML won't allow to load an image with a name like `ç.png`
Cybrix
@Cybrix Depending on your file system, it could be both.
mattbasta
A: 

I would use a mapping such as Percent Encoding aka URL encoding because of various mismatches between the file-system -- imagine the phrase includes "../../etc/passwd" -- and poor (or absent or differeing) unicode-implementations.

Wiki has some about Unicode in HTML; make sure that the correct encoding hints are returned to the browser in Content-Type (e.g. don't rely on the default) and that your FS (and the PHP/web-server access) allow names with Unicode and/or are "talking" the same encoding (UTF-8/percent-encoded, or otherwise).

Also, HTTP/URIs doesn't "understand" Unicode, only ASCII. See Unicode characters in URLs which talks about the common encoding(s) used. Modern browsers will do the encoding automatically (while still showing the unicode glyphs in the location bar, etc.). However, relying on a browser to do this encoding automatically means that unaware browsers will not act properly. Hence, back to paragraph one.

pst