|
-
Feb 27th, 2006, 10:56 PM
#1
Thread Starter
Frenzied Member
str_replace not working?
Must be missing something here but str_replace isnt working for me, take this simple example,
PHP Code:
<?php
$a = "http://somesite.com/index.cfm?&user=24706091";
$b = str_replace("&","&",$a);
echo $b;
?>
Result is still the original url whereas it should be,
http://somesite.com/index.cfm?&user=24706091
-
Feb 27th, 2006, 11:06 PM
#2
Re: str_replace not working?
Did you look at the source code? an & in HTML is viewed as &. It may have changed it but it wouldn't be noticable to you.
-
Feb 28th, 2006, 06:06 PM
#3
Member
Re: str_replace not working?
try preg_replace ( mixed pattern, mixed replacement, mixed subject)
ex:
PHP Code:
<?php
$a = "http://somesite.com/index.cfm?&user=24706091";
$b = preg_replace("&","&",$a);
echo $b;
?>
-
Feb 28th, 2006, 06:15 PM
#4
<?="Moderator"?>
Re: str_replace not working?
 Originally Posted by Jmacp
Must be missing something here but str_replace isnt working for me, take this simple example,
PHP Code:
<?php
$a = "http://somesite.com/index.cfm?&user=24706091";
$b = str_replace("&","&",$a);
echo $b;
?>
Result is still the original url whereas it should be,
http://somesite.com/index.cfm?&user=24706091

That code works fine, like kasracer said check the source. You should also be careful when creating url with & inside, it can cause some things you didnt expect to happen, use urlencode to make sure that you get the exact values you want from the query stirng
-
Mar 1st, 2006, 02:50 PM
#5
Re: str_replace not working?
Its also worth noting that there is a function. htmlspecialchars() which converts all HTML meta characters to their entity equivilents.
The replacement you made inserts a literal ampersand into the query string. Like John has pointed out, you need to use urlencode() on this instead of escaping it as html, a URL encoded string is valid in an HTML attribute string and shouldn't need further escaping.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|