View Full Version : preg_replace results and non results
bluewalrus
06-17-2010, 05:22 AM
Is there a way to get both the found results and the non found results from a preg_replace?
$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
$move_over = preg_replace(!$pattern, $replacement, $pending_files);
but that didn't work I figure there must be some way to do this though...
james438
06-17-2010, 07:08 AM
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.
bluewalrus
06-17-2010, 12:52 PM
I've got 2 files "done", and "not done".
Not done contains something like
<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
<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
<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.
bluewalrus
06-17-2010, 08:12 PM
So this does it but I figure there must be another way rather than running the same search twice...
$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];
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.