mwhym Posted April 3, 2008 Share Posted April 3, 2008 Hello, I am using preg_replace to change the content retrieved from a file, but I would like to incrementally mark the change of each occurence. For instance : have the text : <td class="bodyText" align="center">01/04/2008</td> <td class="bodyText" align="center">6,70</td> <td class="bodyText" align="right">5,02</td> <td class="bodyText" align="right">2.430</td> <td class="bodyText" align="center">6,72</td> <td class="bodyText" align="center">6,48</td> <td class="bodyText" align="right">15.942,22</td> <td class="bodyText" align="right">20</td> <td class="bodyText" align="center">6,48</td> in a file retrieved with fread in a variable called $sul and use : $sul = preg_replace('#<td class=\"(.*?)\" align=(.*?)>#',' JAZZ ', $sul); This will return : JAZZ 01/04/2008</td> JAZZ 6,70</td> JAZZ 5,02</td> JAZZ 2.430</td> JAZZ 6,72</td> JAZZ 6,48</td> JAZZ 15.942,22</td> JAZZ 20</td> JAZZ 6,48</td> Now what i would like to achieve (if possible) is to have each replacement incrementally marked during the preg_replace, so as to arrive to something like : JAZZ1 01/04/2008</td> JAZZ2 6,70</td> JAZZ3 5,02</td> JAZZ4 2.430</td> JAZZ5 6,72</td> JAZZ6 6,48</td> JAZZ7 15.942,22</td> JAZZ8 20</td> JAZZ9 6,48</td> Is this possible ? I am not even sure I am explaining this properly I hope my question is getting through. I tried to use something like the following to see if i could do an incremental change : do { $i++; $sul=preg_replace("/BRUS/", $i,$sul); } while ($i<10); But it returns it without incrementing. 1 01/04/2008</td> 1 6,70</td> 1 5,02</td> 1 2.430</td> 1 6,72</td> 1 6,48</td> 1 15.942,22</td> 1 20</td> 1 6,48</td> If anyone could help me out with this I would greatly appreciate it Thanks George Quote Link to comment https://forums.phpfreaks.com/topic/99344-solved-preg_replace/ Share on other sites More sharing options...
MadTechie Posted April 3, 2008 Share Posted April 3, 2008 what about something like <?php do { $i++; $sul=preg_replace('<td class=\"(?:.*?)\" align=(?:.*?)>', "JAZZ".$i,$sul, 1); }while ($i<10); ?> EDIT: updated RegEx Quote Link to comment https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508295 Share on other sites More sharing options...
MiCR0 Posted April 3, 2008 Share Posted April 3, 2008 Or you could just use explode on the </td> and then echo them out from the array. Quote Link to comment https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508297 Share on other sites More sharing options...
MadTechie Posted April 3, 2008 Share Posted April 3, 2008 but then your need to remove the class align etc .. seams longer to me! Quote Link to comment https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508300 Share on other sites More sharing options...
mwhym Posted April 3, 2008 Author Share Posted April 3, 2008 Thanks for the input much appreciated. I tried it out but it confused me a bit. As you pasted it - it replaces the first occurence only and returns it like : JAZZ10 01/04/2008</td> <td class="bodyText" align="center">6,70</td> <td class="bodyText" align="right">5,02</td> <td class="bodyText" align="right">2.430</td> <td class="bodyText" align="center">6,72</td> <td class="bodyText" align="center">6,48</td> <td class="bodyText" align="right">15.942,22</td> <td class="bodyText" align="right">20</td> <td class="bodyText" align="center">6,48</td> -------------------------------------------------- I tried to differentiate it a bit : <?php do { $i++; $sul=preg_replace('#<td class=\"(?:.*?)\" align=(?:.*?)>#', "JAZZ".$i,$sul, 10); }while ($i<10); ?> This almost worked changed the first instance to JAZZ1 - then the next 10 to JAZZ2 - then the next 10 to JAZZ3 LIKE : JAZZ1 01/04/2008</td> JAZZ2 6,70</td> JAZZ2 5,02</td> JAZZ2 2.430</td> JAZZ2 6,72</td> JAZZ2 6,48</td> JAZZ2 15.942,22</td> JAZZ2 20</td> JAZZ2 6,48</td> JAZZ2 01/04/2008</td> JAZZ2 6,70</td> JAZZ3 5,02</td> JAZZ3 2.430</td> JAZZ3 6,72</td> JAZZ3 6,48</td> JAZZ3 15.942,22</td> JAZZ3 20</td> JAZZ3 6,48</td> Its a step forward though thanks will keep wrestling with it Quote Link to comment https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508307 Share on other sites More sharing options...
MadTechie Posted April 3, 2008 Share Posted April 3, 2008 try this <?php do { $i++; $sul=preg_replace('#<td class=\"(?:.*?)\" align=(?:.*?)>#', "JAZZ".$i,$sul, 1); }while ($i<10); ?> Quote Link to comment https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508309 Share on other sites More sharing options...
mwhym Posted April 3, 2008 Author Share Posted April 3, 2008 Still with this last bit of code it only replaces the first instance with JAZZ10 and then ignores the rest. Thanks alot. George Quote Link to comment https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508311 Share on other sites More sharing options...
laffin Posted April 3, 2008 Share Posted April 3, 2008 use preg_replace_callback <?php $counter=0; function UpdCounter($matches) { global $counter; return "JAZZ" .++$counter. $matches[1]; } $sul=preg_replace_callback('#<td class=\"(.*?)\" align=(.*?)>#',' JAZZ ', $sul) ?> Quote Link to comment https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508312 Share on other sites More sharing options...
laffin Posted April 3, 2008 Share Posted April 3, 2008 oops in my haste messed up the conversion to using preg_replace_callback it shud be $sul=preg_replace_callback('#<td class=\"(.*?)\" align=(.*?)>#',' UpdCounter', $sul) Quote Link to comment https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508314 Share on other sites More sharing options...
mwhym Posted April 3, 2008 Author Share Posted April 3, 2008 Ahhh thanks so much This last bit worked like a charm (only prob was the single quotes around the function in the preg_replace) just updated it to : $sul=preg_replace_callback('#<td class=\"(.*?)\" align=(.*?)>#',UpdCounter, $sul) One things (more of a curiosity as it doesnt really matter for my purposes) why does it start the count at 10 ? Starts at JAZZ10 and then increments by one normally - but cant figure out why it starts at 10 and not 1. Thanks a million for the help everyone much appreciated. George Quote Link to comment https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508321 Share on other sites More sharing options...
MadTechie Posted April 3, 2008 Share Posted April 3, 2008 <?php $sul = 'JAZZ10 01/04/2008</td> <td class="bodyText" align="center">6,70</td> <td class="bodyText" align="right">5,02</td> <td class="bodyText" align="right">2.430</td> <td class="bodyText" align="center">6,72</td> <td class="bodyText" align="center">6,48</td> <td class="bodyText" align="right">15.942,22</td> <td class="bodyText" align="right">20</td> <td class="bodyText" align="center">6,48</td>'; $i=0; do { $i++; $sul=preg_replace('#<td class=\"(?:.*?)\" align=(?:.*?)>#', "JAZZ".$i." - ",$sul, 1); }while ($i<10); echo $sul; ?> returns JAZZ10 01/04/2008</td> JAZZ1 - 6,70</td> JAZZ2 - 5,02</td> JAZZ3 - 2.430</td> JAZZ4 - 6,72</td> JAZZ5 - 6,48</td> JAZZ6 - 15.942,22</td> JAZZ7 - 20</td> JAZZ8 - 6,48</td> I guess i don't know what your asking for coz that looks correct to me! Quote Link to comment https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508322 Share on other sites More sharing options...
mwhym Posted April 3, 2008 Author Share Posted April 3, 2008 Sigh ... you are right... i am being stupid Sorry for the confusion the error was due to how I was outputing the result. It does work properly... Sorry again and thanks alot for the help. Quote Link to comment https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508332 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.