Jump to content

[SOLVED] preg_replace - Replacing every other...


ripkjs

Recommended Posts

I have a file that is exported as a table in an .html file. I need to replace every other instance of <tr> with a new string within that file. The most there will be in the file at any given time will never be more than around 100, if that matters. Any ideas? I am pretty new to php, so all patience is appreciated!

Here's some pseudo-code

 

<?php

$open_file = ...

$odd = 0;

while ($f = fread($file..) {
   if (odd == 0) {
       echo 'this is what happens the first time';
       $odd = 1;
   } else {
       echo 'this is what happens the second time';
       $odd = 0;
   }
}

I'm completely new to PHP and scripting in general. This is pretty much my first attempt at actually writing code. With that said, so after a bit of thinking and reading the php manual. This is what I have come up with so far. What I am ATTEMPTING to do is load points.html as a string, then search that string for '<TR>' and count how many there are. Then do a 'while $trCount > 1' it would then replace the first instance of the string with $trReplace1. Then search again for the first instance in the string (where the one that was the first no longer matches) and replace it with $trReplace2. Then loop back and repeat the process until all of the $tr are replaced. Am I on the right track so far? I'm trying to learn, so no writing the code for me! Just give me a swift shove towards the right door  ;D

 

Also, if someone could help me understand how to actually display the contents of points.html with all the updated replacements, I would be very greatful.

 

<?php

$file = file_get_contents("points.html");
$tr = '<TR>'
$trReplace1 = '<TR class="row1">'
$trReplace2 = '<TR class="row2">'
$trCount = substr_count($file, $tr);
$trFirst = strstr($file, $tr);

preg_replace ($tr, $trReplace1, $trFirst)

?>

Before:

 

<TR><TH>name</TH><TH>header1</TH><TH>header2</TH><TH>header3</TH><TH>header4</TH><TH>header5</TH></TR>
<TR><TD>name1</TD><TD>7</TD><TD><BR></TD><TD>-60</TD><TD><BR></TD><TD>30</TD></TR>
<TR><TD>name2</TD><TD>7</TD><TD><BR></TD><TD>96</TD><TD><BR></TD><TD>161</TD></TR>
<TR><TD>name3</TD><TD>7</TD><TD><BR></TD><TD>82</TD><TD><BR></TD><TD>285</TD></TR>
<TR><TD>name4</TD><TD>7</TD><TD><BR></TD><TD>39</TD><TD><BR></TD><TD>59</TD></TR>

 

After:

 

<TR class="row1"><TH>name</TH><TH>header1</TH><TH>header2</TH><TH>header3</TH><TH>header4</TH><TH>header5</TH></TR>
<TR class="row2"><TD>name1</TD><TD>7</TD><TD><BR></TD><TD>-60</TD><TD><BR></TD><TD>30</TD></TR>
<TR class="row1"><TD>name2</TD><TD>7</TD><TD><BR></TD><TD>96</TD><TD><BR></TD><TD>161</TD></TR>
<TR class="row2"><TD>name3</TD><TD>7</TD><TD><BR></TD><TD>82</TD><TD><BR></TD><TD>285</TD></TR>
<TR class="row1"><TD>name4</TD><TD>7</TD><TD><BR></TD><TD>39</TD><TD><BR></TD><TD>59</TD></TR>

 

Basically the application that exports this data, exports it as an HTML table. And I'd like to just toss this HTML file in a directory on the server and have the script go through it and change the style of every other row. Need to alternate between two colors to make the table easier to read while I'd be removing all the borders via CSS.

This is what I have so far..

 

<?php

$tr = '<TR>';
$trReplace1 = '<TR class="row1">';
$trReplace2 = '<TR class="row2">';
$file = file_get_contents("points.html");
$trCount = substr_count($file, $tr);
$trFirst = strstr($file, $tr);

while ($trCount > 0) {
preg_replace ($tr, $trReplace1, $trFirst);
preg_replace ($tr, $trReplace2, $trFirst);
}


?>

 

When I run it, there is finally no errors, but it also (obviously) doesn't display anything yet, so I'm not sure what its doing.  :-[

Here's a really dumb way of doing it.

 

(in python):

 

str = """<TR><TH>name</TH><TH>header1</TH><TH>header2</TH><TH>header3</TH><TH>header4</TH><TH>header5</TH></TR>
<TR><TD>name1</TD><TD>7</TD><TD><BR></TD><TD>-60</TD><TD><BR></TD><TD>30</TD></TR>
<TR><TD>name2</TD><TD>7</TD><TD><BR></TD><TD>96</TD><TD><BR></TD><TD>161</TD></TR>
<TR><TD>name3</TD><TD>7</TD><TD><BR></TD><TD>82</TD><TD><BR></TD><TD>285</TD></TR>
<TR><TD>name4</TD><TD>7</TD><TD><BR></TD><TD>39</TD><TD><BR></TD><TD>59</TD></TR>"""

odd = 0
for i in str.split('<TR>')[1:]:
    if odd == 0:
        print '<tr class="row1">%s' % i
        odd = 1
    else:
        print '<tr class="row2">%s' % i
        odd = 0

 

instead of using split use explode -- http://php.net/explode

 

Or, do it in python and replace str with:

 

f = open('points.html', 'r')
str = "".join(f.readlines())
f.close()

.
.
.
rest of code from above

 

Outputs:

 

<tr class="row1"><TH>name</TH><TH>header1</TH><TH>header2</TH><TH>header3</TH><TH>header4</TH<TH>header5</TH></TR>
<tr class="row2"><TD>name1</TD><TD>7</TD><TD><BR></TD><TD>-60</TD><TD><BR></TD><TD>30</TD></TR>
<tr class="row1"><TD>name2</TD><TD>7</TD><TD><BR></TD><TD>96</TD><TD><BR></TD><TD>161</TD></TR>
<tr class="row2"><TD>name3</TD><TD>7</TD><TD><BR></TD><TD>82</TD><TD><BR></TD><TD>285</TD></TR>
<tr class="row1"><TD>name4</TD><TD>7</TD><TD><BR></TD><TD>39</TD><TD><BR></TD><TD>59</TD></TR>

 

str = """<TR><TH>name</TH><TH>header1</TH><TH>header2</TH><TH>header3</TH><TH>header4</TH><TH>header5</TH></TR>
<TR><TD>name1</TD><TD>7</TD><TD><BR></TD><TD>-60</TD><TD><BR></TD><TD>30</TD></TR>
<TR><TD>name2</TD><TD>7</TD><TD><BR></TD><TD>96</TD><TD><BR></TD><TD>161</TD></TR>
<TR><TD>name3</TD><TD>7</TD><TD><BR></TD><TD>82</TD><TD><BR></TD><TD>285</TD></TR>
<TR><TD>name4</TD><TD>7</TD><TD><BR></TD><TD>39</TD><TD><BR></TD><TD>59</TD></TR>"""

odd = 0
for i in str.split('<TR>')[1:]:
    if odd == 0:
        print '<tr class="row1">%s' % i
        odd = 1
    else:
        print '<tr class="row2">%s' % i
        odd = 0

 

So can I replace the

 

str = """<TR><TH>name</TH><TH>header1</TH><TH>header2</TH><TH>header3</TH><TH>header4</TH><TH>header5</TH></TR>
<TR><TD>name1</TD><TD>7</TD><TD><BR></TD><TD>-60</TD><TD><BR></TD><TD>30</TD></TR>
<TR><TD>name2</TD><TD>7</TD><TD><BR></TD><TD>96</TD><TD><BR></TD><TD>161</TD></TR>
<TR><TD>name3</TD><TD>7</TD><TD><BR></TD><TD>82</TD><TD><BR></TD><TD>285</TD></TR>
<TR><TD>name4</TD><TD>7</TD><TD><BR></TD><TD>39</TD><TD><BR></TD><TD>59</TD></TR>"""

 

With a link to the .html file? those 5 rows are just a small portion of the code, and the contents of the .html will be changing daily.

Alrighty, I think this is what you meant...

 

$points = "points.html";
$getPoints = file_get_contents($points);
$tr = '<TR>';

print_r(explode($tr, $getPoints, -1));

 

When executed it prints the array (did this to have confirmation that I did it right). On the right track?

No worries, I actually appreciate it not being in php! I'm slowly learning! Now that I have the array, I need to do something similar to your:

 

odd = 0
for i in str.split('<TR>')[1:]:
   if odd == 0:
       print '<tr class="row1">%s' % i
       odd = 1
   else:
       print '<tr class="row2">%s' % i
       odd = 0

 

to add the '<tr class="x">'. I'm not quite sure how that code above works, but I understand that it is setting 'odd' to 0 initially, adding a string to the front on the first line, as well as changing 'odd' to 1. Clever way of alternating! Now to figure out how to apply this to my array, then print the results!

 

/scratch head

Or:

<?php
$points = "points.html";
$getPoints = file_get_contents($points);
function odd_replace($matches) {
    static $count = 0;
    if ($count % 2 == 0) { //even
       $count++;
       return '<tr class="row1">';
    }
    else {
       $count++;
       return '<tr class="row2">';
   }
}
$getPoints = preg_replace_callback("/<tr>/i", "odd_replace", $getPoints);
echo $getPoints;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.