Jump to content

problems with arrays


jasonc

Recommended Posts

I am not sure how i correctly create or access the array fields, can someone please check my code and advise what i may be doing wrong as the $textstring is not getting changed at all.

 

emoticons.php

<?
// i have given just few of the icons as its a long file.
$emoticons = array(
'0:-)' => 'angel.gif',
'>:-(' => 'angry.gif>', 
'(applause)' => 'applause.gif', 
':-)' => 'biggthumpup.gif', 
'(bow)' => 'bow.gif', 
':-)' => 'censored.gif', 
'(clap)' => 'clap_hands.gif', 
':-)' => 'confused.gif'
)

 

main.php

<?
include("emoticons.php");
for ($i = 0; $i < count($emoticons); $i++) {
?><a href="#" onclick="AddSmiley('<?=$emoticons[$i][0];?>');return false"><img src="images/emoticons_<?=$emoticons[$i][1];?>"></a><?
}


function convert_emoticons($textstring) {
include("emoticons.php");
for ($i = 0; $i < count($emoticons); $i++) {
$textstring = str_replace($emoticons[$i][0], '<img src="images/emoticons_' . $emoticons[$i][1] . '>', $textstring);
}

return ($textstring);
}

?>

Link to comment
https://forums.phpfreaks.com/topic/199618-problems-with-arrays/
Share on other sites

Your problem is that you're trying to access the members of the array as if it's a multidimensional array, which it's not.

 

function convert_emoticons($textstring){
include("emoticons.php");
foreach($emoticons as $symbol => $filename)
{
	$textstring = str_replace($symbol, '<img src="images/emoticons_' . $filename . '>', $textstring);
}
return $textstring;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.