FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » str_replace, replace array with string AND array
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: str_replace, replace array with string AND array [message #178784 is a reply to message #178783] Fri, 03 August 2012 06:29 Go to previous messageGo to previous message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma:
Senior Member
On 8/3/2012 7:28 AM, Jason C wrote:
> I'm doing this:
>
> $emArr = array(
> ":-)",
> ":-D",
> ":-(",
> ":-o",
> ":-x",
> ":-P",
> ":-?",
> "8-)"
> );
>
> $imgArr = array(
> "<img src=\"$image_path/emoticons/AddEmoticons08021.gif\">",
> "<img src=\"$image_path/emoticons/AddEmoticons08047.gif\">",
> "<img src=\"$image_path/emoticons/AddEmoticons08017.gif\">",
> "<img src=\"$image_path/emoticons/AddEmoticons08027.gif\">",
> "<img src=\"$image_path/emoticons/AddEmoticons08042.gif\">",
> "<img src=\"$image_path/emoticons/AddEmoticons08050.gif\">",
> "<img src=\"$image_path/emoticons/AddEmoticons08044.gif\">",
> "<img src=\"$image_path/emoticons/AddEmoticons08046.gif\">"
> );
>
> $this_comment = str_replace($emArr, $imgArr, $this_comment);
>
> Just because it would save a few bytes and be easier to read, I would like to change $imgArr to just this:
>
> $imgArr = array(
> "AddEmoticons08021.gif",
> "AddEmoticons08047.gif",
> "AddEmoticons08017.gif",
> "AddEmoticons08027.gif",
> "AddEmoticons08042.gif",
> "AddEmoticons08050.gif",
> "AddEmoticons08044.gif",
> "AddEmoticons08046.gif"
> );
>
> and then change the str_replace to something like this:
>
> $this_comment = str_replace($emArr,
> "<img src=\"$image_path/emoticons/" . $imgArr . "\">",
> $this_comment);
>
> I tried it, of course, but instead of replacing with the correct value of $imgArr, it just replaced with "Array".
>
> Is there a better function for this, or should I stick with the repeated text in the array?
>


You could try to do it like this:
(I added the arrays together)

$prefix = '<img src="$image_path/emoticons/';
$postfix = '">';

$emArr = array(
":-)" => $prefix."AddEmoticons08021.gif".$postfix,
":-D" => $prefix."AddEmoticons08047.gif".$postfix,
":-(" => $prefix."AddEmoticons08017.gif.$postfix
// etc. etc.
);

And then
$this_comment = str_replace
(array_keys($emArr), $emArr, $this_comment);

Regards,
Erwin Moller



--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: Is my array understanding correct?
Next Topic: Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 11:43:22 GMT 2024

Total time taken to generate the page: 0.04137 seconds