SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    Chicago, USA
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_replace() Help

    I'm trying to do something like this for preg_replace():

    Look in $string for a '$' followed by at least one letter/number/underscore followed by '[' followed by a single quote followed by at least one letter/number/underscore followed by another single quote and then finally a ']' character (yeah, kind of a mouth-full, lol).

    So if $string contained this:

    Blah Blah $array1['something'] Blah Blah $array2['something_else'] Blah Blah

    Then $string would be replaced with:

    Blah Blah Value Of $array1['something'] Blah Blah Value Of $array2['something_else'] Blah Blah

    Could anyone help me with this? I tried something like this:

    $string = preg_replace ("#\$([a-ZA-Z0-9\_]+\[\'[a-zA-Z0-9\_]+\'\])#", "$\\1", $string);

    And it didn't work.

    Help appreciated.

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a working preg_match_all example:

    PHP Code:
    <pre>
    <?php

    $string 
    '$array1[\'something\'] Blah Blah $array2[\'something_else\'] Blah Blah';

    preg_match_all('#\$[a-z0-9_]+?\[\'[a-z0-9_]+?\']#is'$string$match);

    print_r($match);

    ?>
    </pre>

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    Chicago, USA
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Chris82
    Here is a working preg_match_all example:

    PHP Code:
    <pre>
    <?php

    $string 
    '$array1[\'something\'] Blah Blah $array2[\'something_else\'] Blah Blah';

    preg_match_all('#\$[a-z0-9_]+?\[\'[a-z0-9_]+?\']#is'$string$match);

    print_r($match);

    ?>
    </pre>
    Thanks, but one problem. I should've mentioned this before, but I'm trying to do something like this:

    Code:
       ...
    
       $contents = file ($file);
    
       foreach ($contents as $line) {
          $line = preg_replace ('#\$([a-z0-9_]+?\[\'[a-z0-9_]+?\'])#is', "$\\1", $line);
          $new_contents .= $line;
       }
    
       return $new_contents;
    
       ...
    The file that I'm getting the contents from looks something like this:

    Some Content $array['something'] More Content
    What I'm trying to do is replace all instances of an associative array element call in that file with the value of that associative array element.

    Any ideas? I tried using the preg_match_all(), but I'm not sure how I would be able to apply to this situation I have here.

  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using this works:

    PHP Code:
    <pre>
    <?php

    $data 
    file('test.txt');

    $array1['something'] = 42;
    $array2['something_else'] = 'This work';

    $max count($data);
    for (
    $i 0$i $max$i++) {
       
    #preg_match_all("#(\\\$[a-z0-9_]+?)\['([a-z0-9_]+?)'\]#is", $data[$i], $match);
       #print_r($match);
       
    $data[$i] = preg_replace("#(\\\$[a-z0-9_]+?)\['([a-z0-9_]+?)'\]#eis""$1['$2']"$data[$i]);
       echo 
    $data[$i];
    }


    ?>
    </pre>


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
  •