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 :: Unread Messages :: Show Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
str_replace, replace array with string AND array [message #178783] Fri, 03 August 2012 01:28 Go to next message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma: 0
Member
add to buddy list
ignore all messages by this user
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?
Re: str_replace, replace array with string AND array [message #178784 is a reply to message #178783] Fri, 03 August 2012 02:29 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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
Re: str_replace, replace array with string AND array [message #178790 is a reply to message #178784] Fri, 03 August 2012 22:38 Go to previous message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma: 0
Member
add to buddy list
ignore all messages by this user
On Friday, August 3, 2012 2:29:32 AM UTC-4, Erwin Moller wrote:
> 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);


That works, thanks! And especially thanks for the tip on using the hash array; I didn't think about using array_keys(). That's much easier to read!
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
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: Sun Oct 22 22:43:50 EDT 2017

Total time taken to generate the page: 0.00660 seconds