...

View Full Version : Issues with str_replace



CyberPirate
03-26-2009, 09:48 PM
I'm trying to convert text into pictures for using it as gender signs on my website.

This is my code:

$gendertext = array("female", "male");
$genderimg = array("<img src=\"img/sex-female.gif\" border=\"0\">", "<img src=\"img/sex-male.gif\" border=\"0\">");
$gender = str_replace($gendertext, $genderimg, $userrow['gender']);

it does convert "male" to the gender icon, but the female icon turns out like this:

.gif" border="0">

It's obviously that it converts every word with the word "male" in it first which make the "female" word converted too. Anyone have a solution for this? I am too familiar with regex so I wouldn't use that :P

Fumigator
03-26-2009, 11:15 PM
I would do "quick-n-dirty" rather than spend any time coming up with a "clever" or "elegant" way.

1. str_replace all "female" occurances with a wacky string, such as "!!%&omg_no_way()::".
2. str_replace all "male" occurances with your male image.
3. str_replace all "!!%&omg_no_way()::" with your female image.

oesxyl
03-27-2009, 02:26 AM
I'm trying to convert text into pictures for using it as gender signs on my website.

This is my code:

$gendertext = array("female", "male");
$genderimg = array("<img src=\"img/sex-female.gif\" border=\"0\">", "<img src=\"img/sex-male.gif\" border=\"0\">");
$gender = str_replace($gendertext, $genderimg, $userrow['gender']);

it does convert "male" to the gender icon, but the female icon turns out like this:

.gif" border="0">

It's obviously that it converts every word with the word "male" in it first which make the "female" word converted too. Anyone have a solution for this? I am too familiar with regex so I wouldn't use that :P
why don't you just add ""<img src=\"img/sex-" before and ".gif\" border=\"0\">" after what you have in $gendertext ?

best regards

CyberPirate
03-27-2009, 10:18 AM
why don't you just add ""<img src=\"img/sex-" before and ".gif\" border=\"0\">" after what you have in $gendertext ?

best regards

I'm not too sure if I understand what you mean?

timgolding
03-27-2009, 10:36 AM
I'm not too sure if I understand what you mean?

He means this



echo '<img src="img/sex-" .$userrow['gender']. ".gif" border="0">';


Your string replace looks wrong. whats in $userrow['gender'] thats the string that is being replaced. The third argument should be $genderimg that the variable with the string to be replaced that should be a string not an array and would have an wacky string as Fumigator put it. I always incase these in curly brackets. The first argument in str_replace should be that wacky string. Then the second argument is the gender you wish to change to



<?PHP

$genderimg = array("<img src=\"img/sex-{GENDER}.gif\" border=\"0\">");
$gender = str_replace("{GENDER}", $userrow['gender'], genderimg);
?>


Anyway don't need to do it use method one as Oesxyl suggests

F-b0mb
03-27-2009, 12:47 PM
echo '<img src="img/sex-'.$userrow['gender'].'.gif" border="0">';
Just correcting Tim's double quotes where you need single quotes.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum