ripkjs Posted April 17, 2009 Share Posted April 17, 2009 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 https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/ Share on other sites More sharing options...
keeB Posted April 17, 2009 Share Posted April 17, 2009 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 https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812079 Share on other sites More sharing options...
ripkjs Posted April 17, 2009 Author Share Posted April 17, 2009 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 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 https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812613 Share on other sites More sharing options...
keeB Posted April 17, 2009 Share Posted April 17, 2009 Yeah I completely screwed up and misread your intent. Can you paste an example points.html and also paste a manual example of what you'd like to do with it? Link to comment https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812638 Share on other sites More sharing options...
ripkjs Posted April 17, 2009 Author Share Posted April 17, 2009 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 https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812653 Share on other sites More sharing options...
ripkjs Posted April 17, 2009 Author Share Posted April 17, 2009 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 https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812657 Share on other sites More sharing options...
keeB Posted April 17, 2009 Share Posted April 17, 2009 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 https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812662 Share on other sites More sharing options...
ripkjs Posted April 17, 2009 Author Share Posted April 17, 2009 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 https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812675 Share on other sites More sharing options...
keeB Posted April 17, 2009 Share Posted April 17, 2009 Keep in mind that code is in python, not php. Somehow you're going to read that content (points.html) in to a string and explode it. Link to comment https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812679 Share on other sites More sharing options...
ripkjs Posted April 17, 2009 Author Share Posted April 17, 2009 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 https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812741 Share on other sites More sharing options...
keeB Posted April 17, 2009 Share Posted April 17, 2009 Yes! Great. The reason I posted it in python was because I dont have a PHP environment handy Link to comment https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812745 Share on other sites More sharing options...
ripkjs Posted April 17, 2009 Author Share Posted April 17, 2009 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 https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812756 Share on other sites More sharing options...
DarkWater Posted April 17, 2009 Share Posted April 17, 2009 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 https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812758 Share on other sites More sharing options...
ripkjs Posted April 18, 2009 Author Share Posted April 18, 2009 Got it working perfect. Thank you much for your patience and guidance keeB! And thank you, Dark! Link to comment https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812888 Share on other sites More sharing options...
keeB Posted April 18, 2009 Share Posted April 18, 2009 Cool solution Dark Link to comment https://forums.phpfreaks.com/topic/154448-solved-preg_replace-replacing-every-other/#findComment-812900 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.