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!

Link to comment
Share on other sites

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;
   }
}

Link to comment
Share on other sites

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)

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.  :-[

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

Link to comment
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.