...

View Full Version : str_replace



waynenort
08-15-2012, 02:40 PM
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?

Fou-Lu
08-15-2012, 03:27 PM
Works for me.
What version of PHP are you using and what is the string in $matches?

waynenort
08-15-2012, 11:48 PM
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

tangoforce
08-16-2012, 12:27 AM
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 :D)

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 :thumbsup:

safeservicejt
08-16-2012, 05:11 AM
You can use preg_replace :)



<?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;
?>

Fou-Lu
08-16-2012, 05:26 AM
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 &.

waynenort
08-16-2012, 10:17 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum