Forum Moderators: coopster

Message Too Old, No Replies

php (finding and replacing code problem)

please help me out

         

kebrus

11:37 pm on Sep 9, 2004 (gmt 0)

10+ Year Member



i'm using a compilation of php scripts called CuteNews
and they have this function for insert smiles

////////////////////////////////////////////////////////
// Function: insertSmilies
// Description: insert smilies for adding into news/comments

function insertSmilies($insert_location, $break_location = FALSE)
{
global $config_http_script_dir, $config_smilies;
$smilies = explode(",", $config_smilies);
foreach($smilies as $smile)
{
$i++; $smile = trim($smile);
$output .= "<a href=\"javascript:insertext(':$smile:','$insert_location')\"><img style=\"border: none;\ align=\"top\" alt=\"$smile\" src=\"$config_http_script_dir/data/emoticons/$smile.gif\" /></a>";
if($i%$break_location == 0 and $break_location)
{
$output .= "<br />";
}else{ $output .= "&nbsp;"; }
}
return $output;
}

little explanation:
$config_http_script_dir = (example: [example.com...]
$config_smilies = is where the smiles codes are stored (example: smile,wink,tongue,evillaugh )
$smilies = will separate the words by comas
$smile = is the final code (example: smile )

my problem is that i want to insert a new variable called $code which in the $output will be placed instead of $smile only in one part to look like this
$output .= "<a href=\"javascript:insertext('$code','$insert_location')\"><img style=\"border: none;\ align=\"top\" alt=\"$smile\" src=\"$config_http_script_dir/data/emoticons/$smile.gif\" /></a>";

and this is what i came to

////////////////////////////////////////////////////////
// Function: insertSmilies
// Description: insert smilies for adding into news/comments

function insertSmilies($insert_location, $break_location = FALSE)
{
global $config_http_script_dir, $config_smilies;
$codigo = explode(",", $config_smilies_codes);
foreach($codigo as $code)
$smilies = explode(",", $config_smilies);
foreach($smilies as $smile)
{
$i++; $smile = trim($smile); $code = trim($code);

$output .= "<a href=\"javascript:insertext('$code','$insert_location')\"><img style=\"border: none;\ align=\"top\" alt=\"$smile\" src=\"$config_http_script_dir/data/emoticons/$smile.gif\" /></a>";
if($i%$break_location == 0 and $break_location)
{
$output .= "<br />";
}else{ $output .= "&nbsp;"; }
}
return $output;
}

but this is not working, because when this code is exectuted only the first $smile code appers and only the last $code appers which means that i will have a smile.gif image with the code :evillaugh:
btw: $config_smilies_codes = stores the smiles code (example: :),;),:P,:evillaugh: )

please someone help me here
i already ask help at cutenews forums, but noone seems to understand this or just don't want to help me
thank you for your time
-kebrus

[edited by: jatar_k at 5:06 pm (utc) on Sep. 10, 2004]
[edit reason] fixed sidescroll [/edit]

mincklerstraat

7:34 am on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks to me like what you basically want to do is replace the output based on the string $smilies with output based on the string $codigo, yes? Use $codigo instead of $smilies?

First off, you need to list $config_smiles_code in your globals declaration - so that line becomes:
global $config_http_script_dir, $config_smilies, $config_smiles_code;

You have constructed an extra loop outside of the loop that is to take place - which would iterate the inside loop for a whole cycle each time your newly added loop takes place. If you don't understand this, don't worry - I think this probably isn't what you want to do, and that you just want to replace the one value with the other. If this is the case, your code should just look like this:

function insertSmilies($insert_location, $break_location = FALSE)
{
global $config_http_script_dir, $config_smilies;
$codigo = explode(",", $config_smilies_codes);
$i = 0;
$smilies = explode(",", $config_smilies);
foreach($smilies as $smile)
{
$i++; $smile = trim($smile); $codigo[$i] = trim($codigo[$i]);

$output .= "<a href=\"javascript:insertext('$codigo[$i]','$insert_location')\"><img style=\"border: none;\ align=\"top\" alt=\"$smile\" src=\"$config_http_script_dir/data/emoticons/$smile.gif\" /></a>";
if($i%$break_location == 0 and $break_location)
{
$output .= "<br />";
}else{ $output .= "&nbsp;"; }
}
$i++;
return $output;
}

Try it this way ;)

kebrus

4:01 pm on Sep 10, 2004 (gmt 0)

10+ Year Member



thank you that didn't work very well but i manage it to work (my script was dum, i was making a loop inside another loop)

btw: now i'm working with this part of the script, and i get this error message: "Compilation failed: unmatched parentheses at offset 1 in at line 334"
line 334 is "$sourse = preg_replace($find,$replace,$sourse);"
and this happends everytime i use the codes :) or ;) (everytime i use parentheses)

and this is the part of the script i've changed, What is wrong?

    $code = explode(",", $config_smilies_codes);
$sm = 0;
$smilies_arr = explode(",", $config_smilies);
foreach($smilies_arr as $smile){
$smile = trim($smile);
$code[$sm] = trim($code[$sm]);
$find[] = "'$code[$sm]'";
$replace[] = "<img style=\"border: none;\ align=\"top\" alt=\"$smile\" src=\"$config_http_script_dir/data/emoticons/$smile.gif\" />"; $sm++;
}

}

$sourse = preg_replace($find,$replace,$sourse);
return $sourse;
}


ok i think nothing is wrong, the problem is that php script recognises parentheses, so now i know that when $code[$sm] is replaced by :) actually it should be :\) but how can i make the script knows that it should ignore parentheses?