Jump to content

Replacing String characters with images


megalos

Recommended Posts

Hi

 

I am trying to replace characters in a string with images so character 'a' will be replaced with a.jpg etc  I have tried the following code but that doesn't result in the desired effect as the updated string gets it's characters replaced as the script works through it so the 'img' in the first updated string gets replaced with with images and it all ends up quite a mess.  Is there a simpler way to do this?

 

$content = "abc" ;

$content = str_replace("a", "<img src=images/a.jpg >", $content);

$content = str_replace("b", "<img src=images/b.jpg >", $content);

$content = str_replace("c", "<img src=images/c.jpg >", $content);

$content = str_replace("d", "<img src=images/d.jpg >", $content);

$content = str_replace("e", "<img src=images/e.jpg >", $content);

$content = str_replace("f", "<img src=images/f.jpg >", $content);

$content = str_replace("g", "<img src=images/g.jpg >", $content);

$content = str_replace("h", "<img src=images/h.jpg >", $content);

$content = str_replace("i", "<img src=images/i.jpg >", $content);

$content = str_replace("j", "<img src=images/j.jpg >", $content);

$content = str_replace("k", "<img src=images/k.jpg >", $content);

$content = str_replace("l", "<img src=images/l.jpg >", $content);

$content = str_replace("m", "<img src=images/m.jpg >", $content);

$content = str_replace("n", "<img src=images/n.jpg >", $content);

$content = str_replace("o", "<img src=images/o.jpg >", $content);

$content = str_replace("p", "<img src=images/p.jpg >", $content);

$content = str_replace("q", "<img src=images/q.jpg >", $content);

$content = str_replace("r", "<img src=images/r.jpg >", $content);

$content = str_replace("s", "<img src=images/s.jpg >", $content);

$content = str_replace("t", "<img src=images/t.jpg >", $content);

$content = str_replace("u", "<img src=images/u.jpg >", $content);

$content = str_replace("v", "<img src=images/v.jpg >", $content);

$content = str_replace("w", "<img src=images/w.jpg >", $content);

$content = str_replace("x", "<img src=images/x.jpg >", $content);

$content = str_replace("y", "<img src=images/y.jpg >", $content);

$content = str_replace("z", "<img src=images/z.jpg >", $content);

 

 

echo $content ;

 

Any help would be gratefully appreciated.

 

Thanks

Megalos (paul)

 

Link to comment
Share on other sites

lol.. I actually forgot to put the space between the ''. this one will work. Just replace '  ' with whatever you want in case you need smaller or bigger spaces.

$content = str_split("abc");
foreach($content as $letter){
   echo $letter == ' ' ? '  ' : '<img src="images/'.$letter.'.jpg" >';
}

Link to comment
Share on other sites

Hi,

 

Thanks, I ended up with a modification to sit a blank image the same dimensions as the other character images for the spaces.  Really appreciated your help. Sure you have guessed it's all a bit new to me but slowly getting there.  The forum has been a great help.

 

$content = str_split("abc");

foreach($content as $letter){

  echo $letter == ' ' ? '<img src="images/blank.jpg" >' : '<img src="images/'.$letter.'.jpg" >';

}

 

Megalos (Paul)

Link to comment
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.