Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/99344-solved-preg_replace/
Share on other sites

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 :)

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508307
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508321
Share on other sites

<?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!

Link to comment
https://forums.phpfreaks.com/topic/99344-solved-preg_replace/#findComment-508322
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.