Results 1 to 5 of 5

Thread: Array within Regex value

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Array within Regex value

    Is there a way I can find a value in an array from a regular expression value?

    I have an array of users and am trying to call the user in the array from the regular expression but am failing. I thought I'd asked this in the past but can't find it with the search.

    PHP Code:
    $pattern '/class="user">(\d+)<\/td>/';
    $replace "class=\"user\">$user[$1]</td>";
    $user preg_replace($pattern$replace$pending_tasks); 
    I think the $1 is being interrupted as a php variable which I don't want it to be.
    Last edited by bluewalrus; 07-20-2010 at 09:08 PM. Reason: easier to read
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    I am pretty sure you need to escape your quotes in the pattern, but I have not tested it out.

    Try reading up on preg_grep for using regular esxpressions with arrays. I have not had the need to use this function either, but this should point you in the right direction.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I would suggest looping through the array using foreach until you find that a key matches the regex. Maybe a little less efficient, but it shouldn't be too problematic and it's certainly easier to write. Unless, of course, there's something more complex to do that would make this not work.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Hmmm I got it to work using this but I think this isn't the most efficient way. Any suggestions? Thanks.

    PHP Code:
    preg_match_all('/<td class="user">\d+<\/td>/'$pending_tasks$matchesPREG_SET_ORDER);
        foreach (
    $matches as $val) {
            for(
    $i 0$i sizeof($val); ++$i) {
                
    $user_key preg_replace('/<td class="user">(\d+)<\/td>/''$1'$val[$i]);
                
    $pending_tasks str_replace("<td class=\"user\">$user_key</td>""<td class=\"user\">$user[$user_key]</td>"$pending_tasks);
            }
        } 
    Corrections to my coding/thoughts welcome.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It is more code to write, but now that I think more about it, how is this really inefficient to any significant degree? Foreach runs, in general, very quickly, and the only slow part of that code is the regex which would be slow regardless of how you run it. Since you need to have that regex compared to the entire array, this format is basically the same amount of work required to do this using any method.
    The only way it would be more efficient is if a default function were processed at a lower level, perhaps running faster. I don't know if this is the case with PHP, and in fact I don't think it is. I'm not sure though.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •