Highlighting paragraphs like table rows with SimpleXML

June 5th, 2010 by Alex Leave a reply »

I’ll show you how to highlight every odd or even paragraph with PHP5 and SimpleXML. What do i mean? Let’s say we have this html structure:

<p>Hi, this is the first paragraph</p>
<p>And here's the second one</p>

Now imagine we have 100 of these paragraphs, they are generated dynamically and we need to highlight every even paragraph (i.e. 2nd, 4th, 6th). This can be made very simple if we break it up with SimpleXML.
First, let’s wrap the paragraphs with <xml> tag to make sure we have one top level element (this is the XML requirement)

<xml>
<p>Hi, this is the first paragraph</p>
<p>And here's the second one</p>
</xml>

Now, let’s initialize our SimpleXML object (and let’s silence it with @ to make sure we don’t have any errors visible):

$content = "<xml><p>Hi, this if the first paragraph</p><p>And here's the second one</p></xml>";
$xml = @simplexml_load_string($content);

Now, to iterate through all paragraphs, we’ll use the xpath method of the simplexml object. Let’s not forget to set the counter to 0, we’ll use it to check if the row is even or odd. If we want to highlight odd rows, we need to set the counter to 1.

if ($xml) {
	$a = 0;
	foreach($xml->xpath('//p') as $p) {
		echo '<p class="row'.($a%2).'">'.$p.'</p>';
		$a++;
	}
}

As you see, we use the result of $a%2 to output 0 or 1 and, thus, generate a class for paragraph with row0 or row1 name. The output will be as follows:

<p class='row0'>Hi, this if the first paragraph</p>
<p class='row1'>And here's the second one</p>

As you see, this is very simple. If we want to make 3 different row colors we can iterate like this:

if ($xml) {
	$a = 0;
	foreach($xml->xpath('//p') as $p) {
		echo '<p class="row'.($a%3).'">'.$p.'</p>';
		$a++;
	}
}

This will output row0, row1 and row2 class names. We only need to define the appropriate class names in our stylesheets.

The full source may look like this:

$content = '<xml><p align="center">bla bla</p><div><span><p>bla bla</p></span></div><div><p class="somethingelse">aha</p></div></xml>';
$xml = @simplexml_load_string($content);
if ($xml) {
	$a = 0;
	foreach($xml->xpath('//p') as $p) {
		echo '<p class="row'.($a%2).'">'.$p.'</p>';
		$a++;
	}
}

If you are using WordPress and need to highlight paragraphs inside a post or page, you can write it like this:

$content = get_the_content();
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
$content = '<xml>'.$content.'</xml>';
$xml = @simplexml_load_string($content);
...

Additionally, if we want to get only those paragraphs that have align=’center’ we can run the xpath like this:

foreach($xml->xpath('//p[@align="center"]') as $p)

XPath is a very simple and powerful way to access the DOM of HTML or XML document. Enjoy it 🙂

This page can be found by searching for:

graphic highlightingwhat is graphic highlighting



1 comment

  1. MaxBloger says:

    Thanks. This output mode is very interesting and nonstandard.