Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-26-2009, 09:48 PM   PM User | #1
CyberPirate
Regular Coder

 
Join Date: Jan 2009
Location: Norway
Posts: 118
Thanks: 8
Thanked 2 Times in 2 Posts
CyberPirate is an unknown quantity at this point
Issues with str_replace

I'm trying to convert text into pictures for using it as gender signs on my website.

This is my code:
PHP 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:
Code:
.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
CyberPirate is offline   Reply With Quote
Old 03-26-2009, 11:15 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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.
__________________
Fumigator is offline   Reply With Quote
Old 03-27-2009, 02:26 AM   PM User | #3
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by CyberPirate View Post
I'm trying to convert text into pictures for using it as gender signs on my website.

This is my code:
PHP 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:
Code:
.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
oesxyl is offline   Reply With Quote
Old 03-27-2009, 10:18 AM   PM User | #4
CyberPirate
Regular Coder

 
Join Date: Jan 2009
Location: Norway
Posts: 118
Thanks: 8
Thanked 2 Times in 2 Posts
CyberPirate is an unknown quantity at this point
Quote:
Originally Posted by oesxyl View Post
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?
CyberPirate is offline   Reply With Quote
Old 03-27-2009, 10:36 AM   PM User | #5
timgolding
Senior Coder

 
timgolding's Avatar
 
Join Date: Aug 2006
Location: Southampton
Posts: 1,460
Thanks: 89
Thanked 110 Times in 109 Posts
timgolding is on a distinguished road
Quote:
Originally Posted by CyberPirate View Post
I'm not too sure if I understand what you mean?
He means this

PHP Code:
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 Code:
<?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
__________________
You can not say you know how to do something, until you can teach it to someone else.
timgolding is offline   Reply With Quote
Old 03-27-2009, 12:47 PM   PM User | #6
F-b0mb
New Coder

 
Join Date: Mar 2009
Posts: 28
Thanks: 3
Thanked 4 Times in 4 Posts
F-b0mb is an unknown quantity at this point
PHP Code:
echo '<img src="img/sex-'.$userrow['gender'].'.gif" border="0">'
Just correcting Tim's double quotes where you need single quotes.
F-b0mb is offline   Reply With Quote
Users who have thanked F-b0mb for this post:
timgolding (03-27-2009)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:53 AM.


Advertisement
Log in to turn off these ads.