Search code examples
phphtml-entitiescpu-wordletter

replacing letters (from a phrase) with images


$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 can't 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 it's not possible)

thank you


Solution

  • 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" />