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 7 of 7

Thread: str_replace

  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    62
    Thanks
    5
    Thanked 14 Times in 14 Posts

    str_replace

    The & symbol stops str_replace from working.

    Eg:
    This won't work..
    $matches = str_replace("0-0&compID=","IT WORKS", $matches);

    But these will:
    $matches = str_replace("compID=","IT WORKS", $matches);
    or
    $matches = str_replace("0-0","IT WORKS", $matches);

    How can I get the first example to work?

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    17,027
    Thanks
    4
    Thanked 2,668 Times in 2,637 Posts
    Works for me.
    What version of PHP are you using and what is the string in $matches?

  3. #3
    New Coder
    Join Date
    Jul 2012
    Posts
    62
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Using PHP 5.2.6

    string in $matches is http://www.sportingpulse.com/rpt_fixture.cgi?id=17343238&client=0-2307-128783-0-0&compID=223607&a=FIXTURE

  4. #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,913
    Thanks
    91
    Thanked 581 Times in 568 Posts
    Quote Originally Posted by waynenort View Post
    This won't work..
    $matches = str_replace("0-0&compID=","IT WORKS", $matches);
    Yes it will. I've run your code on codepad and it works fine. It may be something to do with your php OR your cross contaminating code / files etc (done that plenty of times myself )

    Here is a link to your code being run on codepad:
    http://codepad.org/9ZK146Aj

    Handy tip for you, always test suspect code on another php enabled machine (such as codepad) to see if the problem can be replicated
    Quote Originally Posted by deathshadow View Post
    So seriously, loosen up that tie, let out the belt, and try relating to normal people on the street instead of the gentleman's club crowd.

  5. #5
    New Coder
    Join Date
    Aug 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    You can use preg_replace

    Code:
    <?php
    $text="http://www.sportingpulse.com/rpt_fixture.cgi?id=17343238&client=0-2307-128783-0-0&compID=223607&a=FIXTURE";
    
    $text=preg_replace('/0\-0\&compID\=/i','IT WORKS',$text);
    
    echo $text;
    ?>

  6. #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    17,027
    Thanks
    4
    Thanked 2,668 Times in 2,637 Posts
    Preg is only useful if there is a reason to pattern match instead of directly replace. If you know the literal string to find and replace, then pattern matching is a waste of resource.

    For the OP, can you check to ensure that your string doesn't use &amp;? You'll need to check the source for the output as HTML will render the &amp; as &.

  7. Users who have thanked Fou-Lu for this post:

    waynenort (08-16-2012)

  8. #7
    New Coder
    Join Date
    Jul 2012
    Posts
    62
    Thanks
    5
    Thanked 14 Times in 14 Posts
    For the OP, can you check to ensure that your string doesn't use &amp;? You'll need to check the source for the output as HTML will render the &amp; as &.
    Legend Fou-Lu.
    I should have checked the source, instead relying on the browser. I can't believe I missed that. Thanks


 

Posting Permissions

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