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 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2006
    Location
    Braga, Portugal
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple string replacement

    Hello. If I have the following array...

    PHP Code:
    <?php
    $parameters 
    = array(
    'store="{STORE}"' => '$tags[\'stores\'][{STORE}]',
    'for="{QUESTION}"' => 'box_send("{QUESTION}");'
    );
    ?>
    I would like that from a string, PHP could replace it. For example...

    PHP Code:
    $string 'My string and for="MY STUFF" wohooo!' 
    Is there anyway that from that array PHP would turn this string into:

    PHP Code:
    $resulting_string 'My string and box_send("MY STUFF");'
    I have been googling and php.net'ing for two days now, and after many attempts and observation of other scripts, I couldn't make it. Can you help me? Thanks in advance!
    PHP Code:
    <?
    $iq 
    "150";
    ?>
    <div align="center">
    Your IQ is <?print $iq?>! Is that low or high?
    </div>

  • #2
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    What you want is regular expressions and preg_replace

    Something like this, if you would like it explained just let me know
    PHP Code:
    $string 'My string and for="MY STUFF" wohooo!';

    // $result = My string and box_send("MY STUFF") wohooo!
    $result preg_replace('/\sfor="(.*)"\s/i'' box_send("$1") '$string); 
    Last edited by mr e; 06-11-2007 at 08:19 PM.

  • #3
    New Coder
    Join Date
    Mar 2006
    Location
    Braga, Portugal
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, yeah. I just needed an explanation becaue I don't quite understand the Perl regular expressions.
    PHP Code:
    <?
    $iq 
    "150";
    ?>
    <div align="center">
    Your IQ is <?print $iq?>! Is that low or high?
    </div>

  • #4
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    Haha no problem, they definitely have a learning curve, and not an easy one at that
    PHP Code:
    preg_replace('/\sfor="(.*)"\s/i'' box_send("$1") '$string); 
    First their are the two /'s, the regex goes between these, so if you were just looking to replace the word "for", your regex would be "/for/"

    Then after the last / there is the "i" modifier, which simple means the search is case insensitive

    So we're searching for a space (represented by \s, which means any kind of space, e.g. a space, tab, newline etc)

    Followed by the string for="

    **Here's our matching pattern inside the ()'s**
    Followed by any character (represented by the period) zero or more times (represented by the *)
    (Zero or more means the string could be empty, as opposed to the + which is one or more, and the ? which is zero or one)

    Followed by the string ", then another space

    Then in our replace, we have the string we want to replace it with, box_send("$1"), where the $1 means the first sub pattern (the pattern inside the ()'s matched, $2 would be the second if we had one, etc)

    Hope this helps some, I know how hard regex's were when I first started playing with them, not the most friendly, but definitely cool
    Last edited by mr e; 06-11-2007 at 10:43 PM.

  • #5
    New Coder
    Join Date
    Mar 2006
    Location
    Braga, Portugal
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh... So I could do:

    PHP Code:
    $parameters = array(

    'store' => '$tags[\'stores\'][$1]',
    'for' => 'box_send("$1");'
    );

    foreach(
    $parameters as $parameter => $replacement) {
    preg_replace('/\s'.$parameter.'="(.*)"\s/i'$replacement$string); 

    Is this correct? I changed the keys and values in the array accordingly. Please tell me if this is correct.
    PHP Code:
    <?
    $iq 
    "150";
    ?>
    <div align="center">
    Your IQ is <?print $iq?>! Is that low or high?
    </div>

  • #6
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    You can actually just give preg_replace two arrays and it'll do it for you

    The only thing I saw wrong was I believe you needed to escape the $ in $tags

    And on second thought, you might not want the last \s otherwise it won't match for="something". because of that period (or comma, whatever)

    I also added the U modifier to make the pattern ungreedy, as it was, if you had more than one " it would match the first, then the very last (aka greedy), instead of the next
    PHP Code:
    // $string = 'This is a store="test", and for="fun"!';

    $find = array('/\sstore="(.*)"/iU''/\sfor="(.*)"/iU');
    $replace = array(' $tags[\'stores\'][$1]'' box_send("$1");');

    $result preg_replace($find$replace$string); 

  • #7
    New Coder
    Join Date
    Mar 2006
    Location
    Braga, Portugal
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Gee, thanks, mr e. You've been a great help
    PHP Code:
    <?
    $iq 
    "150";
    ?>
    <div align="center">
    Your IQ is <?print $iq?>! Is that low or high?
    </div>


  •  

    Posting Permissions

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