SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    str_replace(); again isn't working

    str_replace(); again isn't working
    It's because of the < and > and "

    Does not work
    Code:
    $searchthis = '<body><a href="enablejavascript.php"><img src="enablejavascript.png"></a></body>';
    $find2 = '<body><a href="enablejavascript.php"><img src="enablejavascript.png"></a></body>';
    $rep2 = '';
    $toburn = str_replace($find2, $rep2, $searchthis); //does not work
    echo "$toburn"; //does not work
    Does work
    Code:
    $searchthis = '<body><a href="enablejavascript.php"><img src="enablejavascript.png"></a></body>';
    $find2 = 'enablejavascript';
    $rep2 = '';
    $toburn = str_replace($find2, $rep2, $searchthis); //does work
    echo "$toburn"; //does work

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    When you say it doesnt work, do you get any errors?
    I ran your code as is and got the expected blank page with no source - because it has been replaced!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code works fine.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No it doesn't.

    Code:
    <?php
    $searchthis = '<body><a href="enablejavascript.php"><img src="enablejavascript.png"></a></body>';
    $find2 = '<body><a href="enablejavascript.php"><img src="enablejavascript.png"></a></body>';
    $rep2 = '2';
    $toburn = str_replace($find2, $rep2, $searchthis); //does not work
    ?>
    Doesn't work because of the symbols found in $searchthis and $findthis

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Rather than repeating saying that it "doesn't work". Please provide the expected behaviour and the precise, incorrect behaviour that you're seeing.
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are replacing

    Code:
    <body><a href="enablejavascript.php"><img src="enablejavascript.png"></a></body>
    with an empty string.

    Please get some sleep, some caffeine, and re-read this code, or explain what you DESIRE.
    Cross browser css bugs

    Dan Schulz you will be missed

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Seriously, this EXACT code:
    PHP Code:
    <?php
    $searchthis 
    '<body><a href="enablejavascript.php"><img src="enablejavascript.png"></a></body>';
    $find2 '<body><a href="enablejavascript.php"><img src="enablejavascript.png"></a></body>';
    $rep2 '2';
    $toburn str_replace($find2$rep2$searchthis); //does not work
    echo $toburn;
    ?>
    echo's 2 to the page.

    If its not working for you then maybe its something else in the setup.
    What do you get on the page?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I fixed it by changing (the second line)
    Code:
    	$searchthis = '<body><a href="enablejavascript.php"><img src="enablejavascript.png"></a></body>';
    	$searchthis = htmlspecialchars($searchthis);
    	$find2 = '<body><a href="enablejavascript.php"><img src="enablejavascript.png"></a></body>';
    	$rep2 = 'cake';
    	$result2 = str_replace($find2, $rep2, $searchthis);
    to this

    Code:
    	$searchthis = '<body><a href="enablejavascript.php"><img src="enablejavascript.png"></a></body>';
    	$searchthis = htmlspecialchars_decode($searchthis);
    	$find2 = '<body><a href="enablejavascript.php"><img src="enablejavascript.png"></a></body>';
    	$rep2 = 'cake';
    	$result2 = str_replace($find2, $rep2, $searchthis);
    Thanks for nothing coz I was so noobish.
    It looked the same when I echoed it on screen so I assumed it was the same.

  9. #9
    SitePoint Member
    Join Date
    Feb 2009
    Location
    San Diego, CA
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could have also tried using double quotes instead of single quotes to set a string. Then escape any other double quotes used.

    Code:
    $var = "string 1 with \"double quotes\" escaped...";
    
    echo str_replace("string 1","string 2",$var);
    Last edited by Sabia Design; Feb 5, 2009 at 22:34.
    Sabia Design - Modern Web Development
    PHP MySQL Cities - Need a database of cities for PHP / MySQL?
    San Diego, CA / San Francisco, CA / BS CS / Stanford Engineering

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Close this thread. I figured out what the problem was.

    I was trying to replace html tags with html tags, but what I didn't realise is that I should have used functions such as html_entity_decode and htmlspecialchars to make it work.

    What looked like < was actually &lgt; instead. I had to convert the string I wanted to search in, before I used it. All the strings have to be of the equal level.


Bookmarks

Posting Permissions

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