Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    118
    Thanks
    8
    Thanked 2 Times in 2 Posts

    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

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • #4
    Regular Coder
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    118
    Thanks
    8
    Thanked 2 Times in 2 Posts
    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?

  • #5
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    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.

  • #6
    New Coder
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Thanked 4 Times in 4 Posts
    PHP Code:
    echo '<img src="img/sex-'.$userrow['gender'].'.gif" border="0">'
    Just correcting Tim's double quotes where you need single quotes.

  • Users who have thanked F-b0mb for this post:

    timgolding (03-27-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •