Results 1 to 3 of 3

Thread: replace a string and put the replace string on a variable

  1. #1
    Join Date
    Jun 2011
    Location
    Pearl of the East
    Posts
    54
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default replace a string and put the replace string on a variable

    good day everyone,.

    i want to replace a string and put the replaced string on a variable,.

    ex.

    $str = 'this is the oRiGiNaL text';
    $find = 'original';
    $replace = 'replaced';

    output:
    $str = 'this is the replaced text'; //the normal output of replacing text

    $original = 'oRiGiNaL'; //the replaced text will be moved to a new variable and it's format/case is remain


    thanks in advance,.

    -regicide del feroz

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,287
    Thanks
    97
    Thanked 104 Times in 102 Posts

    Default

    PHP Code:
    <?php
    $str 
    'this is the oRiGiNaL text';
    $pattern='/original/is';
    $replace='replaced';
    preg_match($pattern$str$match);
    $str=preg_replace($pattern,$replace,$str);
    $original="$match[0]";
    echo
    "$original<br>";
    echo
    "$str";
    ?>
    The above will find the first occurrence of your desired text and assign it to the variable $original. All of the occurrences of your desired text will be replaced with the replacement text.

    From how you described your problem PCRE is the only way I can see to go.
    Last edited by james438; 04-24-2012 at 09:27 PM.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. The Following User Says Thank You to james438 For This Useful Post:

    regicidedelferoz (04-25-2012)

  4. #3
    Join Date
    Jun 2011
    Location
    Pearl of the East
    Posts
    54
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default

    thank you sir,.

Bookmarks

Posting Permissions

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