How to replace a substring with another?

Discussion in 'PHP' started by subeeshkk, Jan 28, 2008.

  1. #1
    Hi all ,

    I need to replace a sunstring with another from a string , how to do..?


    I have the both the initial position and the ending position of the substring , with these input i have to do that..?

    Or i have, the starting of the String and the ending position..

    To make it more clear..

    $str = 'First Computer is ABC, you know it';
    $substr = 'is';

    i want to replace 'is ABC ' with 'was XYZ' ?
    Also i know , after ABC , a comma comes, so that i can take that position also,

    In sshort , the start and end pos of a sub string i know , i have to replace..

    Please help me
     
    subeeshkk, Jan 28, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    You can use str_replace:

    
    $str = 'First Computer is ABC, you know it';
    
    $find = "is ABC";
    $replace = "was XYZ";
    
    echo str_replace($find, $replace, $str);
    
    PHP:
    Hope this helps,

    Jay
     
    jayshah, Jan 28, 2008 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    nico_swd, Jan 28, 2008 IP
  4. fairuz.ismail

    fairuz.ismail Peon

    Messages:
    232
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    normally the first solution (using the str_replace) will work with charm..it will search the substring itself without knowing the position...good luck.
     
    fairuz.ismail, Jan 28, 2008 IP