Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Replacing all instances in a string

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 101
    Comment on it

    You can replace all instance in a string using str_replace with the help of str_replace function. In this example, str_replace replaces all instances of abc with xyz like this:

    echo str_replace("abc","xyz","abcdefghijklmn");
    

    the output will become : xyzdefghijklmn

    Str_replace will also be used with arrays.

    $fruitarr=array("apple","banana","orange");
    
    $fruit2arr=array("Apple","Banana","Orange");
    echo str_replace($a,$b,"apple,banana and orange are fruits);
    

    The out put will become : Apple,Banana and Orange are fruits. We can also use substr_replace function which replace part of the string with another string.

    
    echo substr_replace("Apple","Banana",0); // 0 will start replacing at the first character in the string
    
    
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: