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 4 of 4
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    My str_replace() isn't working !

    I have the following code:

    PHP Code:
    echo " | The-Link:$the_link";
            if (
    stripos($the_link"n_")){
               
    $the_link str_replace("n_","",$the_link);
                 
    $link_tp "D";
                 }  
    // endif
            
    else {
               
    $link_tp "S";
                 }  
    // end else     
    echo " | The-Link:$the_link ==  Link-tp:$link_tp"

    The input for $the_link is "n_8"

    And the output is :

    | The-Link:n_8 | The-Link:n_8 == Link-tp:S

    Well I was expecting to get:

    | The-Link:n_8 | The-Link:8 == Link-tp: D

    Can anyone see what I've done wrong ?
    Last edited by jeddi; 03-23-2009 at 05:55 PM.

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Your problem is this statement: stripos($the_link, "n_"). It evaluates to 0 since the first position of the occurrence of the substring n_ is in position 0 (remember, positions in strings and arrays begin with 0). In an IF statement, a value of 0 is the same as false, so your str_replace never gets executed.

    Try this:
    PHP Code:
    if (stripos($the_link"n_") !== false){ 
    The !== makes sure that it only checks for boolean false, rather than values like 0 or "".

    -Shane

  • Users who have thanked TheShaner for this post:

    jeddi (03-23-2009)

  • #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,300
    Thanks
    4
    Thanked 203 Times in 200 Posts
    The big warning in the documentation of the function tells you why you're seeing the results you are:

    This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
    Change your test to

    PHP Code:
            if (stripos($the_link"n_") !== FALSE){ 
    and it will work as expected.
    Dave .... HostMonster for all of your hosting needs

  • #4
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks guys for your quick response


  •  

    Posting Permissions

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