Results 1 to 5 of 5

Thread: str_replace not working?

  1. #1

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    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("&","&amp;",$a);
    echo 
    $b;

    ?>
    Result is still the original url whereas it should be,

    http://somesite.com/index.cfm?&amp;user=24706091


  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: str_replace not working?

    Did you look at the source code? an &amp; in HTML is viewed as &. It may have changed it but it wouldn't be noticable to you.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  3. #3
    Member
    Join Date
    Feb 2006
    Posts
    50

    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("&","&amp;",$a); 
    echo 
    $b

    ?>

  4. #4
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: str_replace not working?

    Quote 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("&","&amp;",$a);
    echo 
    $b;

    ?>
    Result is still the original url whereas it should be,

    http://somesite.com/index.cfm?&amp;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

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width