Damage Posted July 11, 2009 Share Posted July 11, 2009 Ok I'm kind of new to php so this may be a nub question but i haven't been able to find an answer to it yet.. here it is.. I'm trying to put a date on a news post but instead of just typing it in as normal text i want to be able to type it in a string then have a function go over the string and replace each number with a pre-made image Thanks. Link to comment https://forums.phpfreaks.com/topic/165588-parse-a-string-and-replace-each-character-in-it-with-an-image/ Share on other sites More sharing options...
BillyBoB Posted July 11, 2009 Share Posted July 11, 2009 So, are asking us to make a script that just takes a timestamp from a database (i.e.: mysql) and forms imgs from just the time? Just trying to get the details straight here. Link to comment https://forums.phpfreaks.com/topic/165588-parse-a-string-and-replace-each-character-in-it-with-an-image/#findComment-873435 Share on other sites More sharing options...
haku Posted July 11, 2009 Share Posted July 11, 2009 $string = 'string'; $letters = explode('', $string); // this gives you an array with one letter in each element $images = array(); foreach($letters as $letter) { $images[] = '<img src="' . $letter . '.jpg" />'; } $images_string = implode($images); echo $images_string; Link to comment https://forums.phpfreaks.com/topic/165588-parse-a-string-and-replace-each-character-in-it-with-an-image/#findComment-873436 Share on other sites More sharing options...
Damage Posted July 11, 2009 Author Share Posted July 11, 2009 I just got it working =D function DateReplace ($ReplaceText) { $Numbers=array( '1' => "<img src='/images/date/1.gif' />", '2' => "<img src='/images/date/2.gif' />", '3' => "<img src='/images/date/3.gif' />", '4' => "<img src='/images/date/4.gif' />", '5' => "<img src='/images/date/5.gif' />", '6' => "<img src='/images/date/6.gif' />", '7' => "<img src='/images/date/7.gif' />", '8' => "<img src='/images/date/8.gif' />", '9' => "<img src='/images/date/9.gif' />", '0' => "<img src='/images/date/0.gif' />" ); $Date=str_replace(array_keys($Numbers), array_values($Numbers), $ReplaceText); return $Date; } Link to comment https://forums.phpfreaks.com/topic/165588-parse-a-string-and-replace-each-character-in-it-with-an-image/#findComment-873440 Share on other sites More sharing options...
thebadbad Posted July 11, 2009 Share Posted July 11, 2009 @haku You can't use an empty delimiter in explode(). Use str_split() instead (PHP 5). Just in case the OP wanted to explore your method Link to comment https://forums.phpfreaks.com/topic/165588-parse-a-string-and-replace-each-character-in-it-with-an-image/#findComment-873462 Share on other sites More sharing options...
haku Posted July 11, 2009 Share Posted July 11, 2009 Hmm, it appears you're right. I could swear I used that in the past! But I guess not. Link to comment https://forums.phpfreaks.com/topic/165588-parse-a-string-and-replace-each-character-in-it-with-an-image/#findComment-873682 Share on other sites More sharing options...
haku Posted July 11, 2009 Share Posted July 11, 2009 Just went back and checked, and I was mixing languages - you can use an empty delimiter with javscript's split() http://www.w3schools.com/jsref/jsref_split.asp Link to comment https://forums.phpfreaks.com/topic/165588-parse-a-string-and-replace-each-character-in-it-with-an-image/#findComment-873691 Share on other sites More sharing options...
thebadbad Posted July 11, 2009 Share Posted July 11, 2009 Ah, I know the feeling Link to comment https://forums.phpfreaks.com/topic/165588-parse-a-string-and-replace-each-character-in-it-with-an-image/#findComment-873696 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.