how to use str_replace in this code snippet?

Discussion in 'PHP' started by Colleen, Sep 10, 2006.

  1. #1
    I want to rewrite "&" in urls as "&" I figure I need to use str_replace. I've been fiddling with it in the following code but can't get it to work so if anyone knows, perhaps you can guide me here? Thanks.

    $qfull = "select * from dd_banners where BannerType = '468x60' ";
    $rfull = mysql_query($qfull) or die(mysql_error());
    
    if(mysql_num_rows($rfull) > '1')
    {
    	while($afull = mysql_fetch_array($rfull))
    	{
    		$fullBannerID[] = $afull[BannerID];
    	}
    
    	srand ((float) microtime() * 10000000);
    	$fullID = array_rand($fullBannerID);
    
    	$FullBID = $fullBannerID[$fullID];
    
    	//get the selected banner
    	$qfull1 = "select * from dd_banners where BannerID = '$FullBID' ";
    	$rfull1 = mysql_query($qfull1) or die(mysql_error());
    	$afull1 = mysql_fetch_array($rfull1);
    
    	$FullBanner = "<a href=\"$afull1[BannerURL]\"><img src=\"banners/$afull1[BannerFile]\" alt=\"$afull1[AltText]\" /></a>";
    }
    elseif(mysql_num_rows($rfull) == '1')
    {
    	$afull = mysql_fetch_array($rfull);
    	$FullBanner = "<a href=\"$afull[BannerURL]\"><img src=\"banners/$afull[BannerFile]\" alt=\"$afull[AltText]\" /></a>";
    }
    Code (markup):
     
    Colleen, Sep 10, 2006 IP
  2. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #2
    falcondriver, Sep 10, 2006 IP
  3. Colleen

    Colleen Illustrious Member

    Messages:
    6,777
    Likes Received:
    725
    Best Answers:
    1
    Trophy Points:
    430
    #3
    Hey, can you extend on this? I wouldn't know where to put it, etc. Just want to rewrite & to &amp;
     
    Colleen, Sep 11, 2006 IP
  4. Crazy4Bass

    Crazy4Bass Well-Known Member

    Messages:
    174
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    138
    #4
    urlencode will probably change all the special characters to %.

    I'd do something like this, might not be as efficient but it should work.
    
    [B]$afull1[BannerURL] = str_replace('&','&amp;',$afull1[BannerURL]);[/B]
    
    $FullBanner = "<a href=\"$afull1[BannerURL]\"><img src=\"banners/$afull1[BannerFile]\" alt=\"$afull1[AltText]\" /></a>";
    
    Code (markup):
    This should change the values in your array. However, be warned, most browsers and PHP will still interpret that as a literal &. The function works, change the '&amp;' to something like '42' and you'll see it does indeed update the array value.

    I'm sure you have a reason for wanting to do this but in my experience, browsers typically just switch it whenever they want so I always use the literal '&' and never had a problem.

    Hope this helps...
     
    Crazy4Bass, Sep 11, 2006 IP
  5. Colleen

    Colleen Illustrious Member

    Messages:
    6,777
    Likes Received:
    725
    Best Answers:
    1
    Trophy Points:
    430
    #5
    Thanks Crazy4Bass. It is so the site will validate at W3C, http://validator.w3.org/

    I know the browser will parse it as regular "&", that's fine. I will try this out and let you know. I've done it in the past but don't know how to get it going with this particular script.

    EDIT: I guess that's not going to do it. Anyone else know? Thanks! :)
     
    Colleen, Sep 11, 2006 IP
  6. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #6
    can you paste the source and what it gives out at this place and what you want out? Crazy4Bass' code looks fine to me.
     
    falcondriver, Sep 12, 2006 IP
  7. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #7
    Colleen it should work. Do you 'view source' to see if it is &amp; instead of &?
     
    smatts9, Sep 12, 2006 IP
  8. Colleen

    Colleen Illustrious Member

    Messages:
    6,777
    Likes Received:
    725
    Best Answers:
    1
    Trophy Points:
    430
    #8
    I got it all figured out, thanks everyone! Some kind individual even rewrote the code because he said it was loops inside loops and that was bad, it's much cleaner and compact and the &amp; is there now. :)
     
    Colleen, Sep 12, 2006 IP
    GRIM likes this.