Jump to content

Recommended Posts

function BText($string) {

$string = str_replace("a","<img src='images/a.png'/>",$string);
$string = str_replace("b","<img src='images/b.png'/>",$string);
$string = str_replace("c","<img src='images/c.png'/>",$string);
$string = str_replace("d","<img src='images/d.png'/>",$string);
$string = str_replace("e","<img src='images/e.png'/>",$string);
$string = str_replace("f","<img src='images/f.png'/>",$string);
$string = str_replace("g","<img src='images/g.png'/>",$string);
$string = str_replace("h","<img src='images/h.png'/>",$string);
$string = str_replace("i","<img src='images/i.png'/>",$string);
$string = str_replace("j","<img src='images/j.png'/>",$string);
$string = str_replace("k","<img src='images/k.png'/>",$string);
$string = str_replace("l","<img src='images/l.png'/>",$string);
$string = str_replace("m","<img src='images/m.png'/>",$string);
$string = str_replace("n","<img src='images/n.png'/>",$string);
$string = str_replace("o","<img src='images/o.png'/>",$string);
$string = str_replace("p","<img src='images/p.png'/>",$string);
$string = str_replace("q","<img src='images/q.png'/>",$string);
$string = str_replace("r","<img src='images/r.png'/>",$string);
$string = str_replace("s","<img src='images/s.png'/>",$string);
$string = str_replace("t","<img src='images/t.png'/>",$string);
$string = str_replace("u","<img src='images/u.png'/>",$string);
$string = str_replace("v","<img src='images/v.png'/>",$string);
$string = str_replace("w","<img src='images/w.png'/>",$string);
$string = str_replace("x","<img src='images/x.png'/>",$string);
$string = str_replace("y","<img src='images/y.png'/>",$string);
$string = str_replace("z","<img src='images/z.png'/>",$string);

$string = nl2br($string);

return $string;
}

I'm currently using this to replace every letter with an image. However sometimes it doesn't work correctly.

Basically it would replace the word "hia" with "<img src='images/h.png'/><img src='images/i.png'/><<img src='images/i.png'/>mg src='images/<<img src='images/h.png'/>mg src='images/i.png'/>.png'/>"

 

When it gets to the letter i it seems to replace it with <img src='images/i.png'/>

Please help.

~Thanks Lars!

Link to comment
https://forums.phpfreaks.com/topic/192061-str_replace-help/
Share on other sites

u shoudl do it in one call like so

 

$search = array('a','b','c');

$replace = array("<img src='a.png'>","<img src='b.png'>","img src='c.png'");

 

$str = str_replace($search, $replace, $string);

 

 

also u may want to look at function str_ireplace which is case insensitive

Link to comment
https://forums.phpfreaks.com/topic/192061-str_replace-help/#findComment-1012272
Share on other sites

damn what about using regexp try this:

 

$result = preg_replace('/([a-z])/i', "<img src='images/\\1.png'>", $string);

 

in case u have different letter pictures for lower and upper chars use this

$result = preg_replace('/([a-zA-Z])/', "<img src='images/\\1.png'>", $string);

Link to comment
https://forums.phpfreaks.com/topic/192061-str_replace-help/#findComment-1012295
Share on other sites

Came up with a solution:

 

function BText($str)
{
    $new = '';

    for($i = 0; $i < strlen($str); $i++)
    {
      if (ctype_alpha($str[$i]))
      {
        $new .= '<img src="images/' . strtolower($str[$i]) . '" />';
      }
      else
      {
        $new .= $str[$i];
      }
    }

  return $new;

}

$str = 'hia, pal!';
$str = BText($str);

echo $str;

 

OUTPUT SOURCE

<img src="images/h" /><img src="images/i" /><imgsrc="images/a" />, <img src="images/p" /><imgsrc="images/a" /><img src="images/l" />!

Link to comment
https://forums.phpfreaks.com/topic/192061-str_replace-help/#findComment-1012337
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.