Results 1 to 4 of 4

Thread: preg_replace results and non results

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

    Default preg_replace results and non results

    Is there a way to get both the found results and the non found results from a preg_replace?

    PHP Code:
    $pattern '/<tr id="'$id_on '">\n\t\t<td>(.*)<\/td>\n\t\t<td>(.*)</td>\n\t\t<td class="user">(.*)<\/td>\n\t<\/tr>/';
    $replacement "";
    $move_over preg_replace($pattern$replacement$pending_files); 

    This is the code I currently have, which pulls the data I want out of the first file but I then want to write it into a second file. I was thinking something like

    PHP Code:
    $move_over preg_replace(!$pattern$replacement$pending_files); 
    but that didn't work I figure there must be some way to do this though...
    Last edited by bluewalrus; 06-17-2010 at 05:22 AM. Reason: using files not records
    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

    Could you explain in a bit more detail what it is you are trying to do? At first glance it sounds like you are trying to use preg_replace() to do a preg_match_all() function and then write it to a file.

    What are the results you are trying to get? Are you trying to retrieve results or change them?

    It is interesting to see someone using \t in a preg_replace. I have not really found a use for it yet. Could you explain where you are using it? I'm rather interested.
    To choose the lesser of two evils is still to choose evil. My personal site

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

    Default

    I've got 2 files "done", and "not done".

    Not done contains something like

    PHP Code:
    <table border="1">
        <
    tr>
            <
    td style="font-weight:bold;">Pending Task</td>
            <
    td style="font-weight:bold;">Date Requested</td>
            <
    td style="font-weight:bold;" id="user">Requestor</td>
        </
    tr>
        <
    tr id="0">
            <
    td>test abc</td>
            <
    td>6/16/10 12:43:18 pm</td>
            <
    td class="user">4</td>
        </
    tr>
    </
    table
    That regex pulls out this part

    PHP Code:
        <tr id="0">
            <
    td>test abc</td>
            <
    td>6/16/10 12:43:18 pm</td>
            <
    td class="user">4</td>
        </
    tr
    and I then rewrite back to the "not done" file something like

    PHP Code:
    <table border="1">
        <
    tr>
            <
    td style="font-weight:bold;">Pending Task</td>
            <
    td style="font-weight:bold;">Date Requested</td>
            <
    td style="font-weight:bold;" id="user">Requestor</td>
        </
    tr>
    </
    table
    or anything else that isn't done. I then want to send the info I removed from that file to the "done" file. Any ideas? Thanks.
    Corrections to my coding/thoughts welcome.

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

    Default

    So this does it but I figure there must be another way rather than running the same search twice...

    PHP Code:
        $pending_files file_get_contents('2bedone.txt');
        
    $pattern '/<tr id="'$id_on '">\n\t\t<td>(.*)<\/td>\n\t\t<td>(.*)<\/td>\n\t\t<td class="user">(.*)<\/td>\n\t<\/tr>/';
        
    $replacement "";
        
    preg_match($pattern$pending_files$matches);
        
    $move_over preg_replace($pattern$replacement$pending_files);
        echo 
    $matches[0]; 
    Corrections to my coding/thoughts welcome.

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
  •