How to bold first (or n-th) paragraph of text

In the last two projects I’ve been asked a simple task to bold first paragraph of text. This task proved to be very complex or quite trivial depending on my customer’s requirements. Hope someone will find code snippets below helpful.

Lets assume your HTML code looks like that:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur imperdiet purus in dictum scelerisque.

Vivamus ut consectetur tortor. Phasellus egestas magna nisi, non tempus purus malesuada sed.

You want to bold first paragraph like this (visual representation depends on CSS):

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur imperdiet purus in dictum scelerisque.

Vivamus ut consectetur tortor. Phasellus egestas magna nisi, non tempus purus malesuada sed.

This can be achieved in at least two ways.

PHP way

In first project I had to not only to bold first paragraph visually but also at syntax level. This proved to be quite difficult. If you are able to pass paragraph formatted text into a $variable this solution might suit you. As I always program with reusability in mind, I encapsulated functions in Utility class. You can put functions below in any existing class, use them standalone or just copy code below straight away. Code explained in comments.

class Utility{ 
	public static function boldFirstPara($string){         
		$dom = new DOMDocument; $dom->loadHTML($string);
		$xPath = new DOMXPath($dom);
		foreach ($xPath->query("//*[contains(@class,'content')]/p[1]") as $p) { // XPath as in CSS3 .content>p:
			$element = $dom->createElement('p');
			$p->parentNode->insertBefore($element, $p->nextSibling);
			$element->appendChild($p);
			Utility::renameElement($p,"strong");
		}
		return $dom->saveHtml();
	}
	// Changes the name of element $element to $newName.
	public static function renameElement($element, $newName) {
		$newElement = $element->ownerDocument->createElement($newName);
		$parentElement = $element->parentNode;
		$parentElement->insertBefore($newElement, $element);
		// copy all nodes to $newElement
		$childNodes = $element->childNodes;
		while ($childNodes->length > 0) {
			$newElement->appendChild($childNodes->item(0));
		}
		// copy attributes to $newElement
		$attributes = $elemen->attributes;
		while ($attributes->length > 0) {
			$attribute = $attributes->item(0);
			if (!is_null($attribute->namespaceURI)) {
				$newElement->setAttributeNS('http://www.w3.org/2000/xmlns/',
				'xmlns:'.$attribute->prefix,
				$attribute->namespaceURI);
			}
			$newElement->setAttributeNode($attribute);
		}

		$parentElement->removeChild($element);
	}
}
$html="
". "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur imperdiet purus in dictum scelerisque. ". "Vivamus ut consectetur tortor. Phasellus egestas magna nisi, non tempus purus malesuada sed. ". "
"; echo Utility::boldFirstPara($html);

CSS3 way

I prefer this solution over PHP one  as bolding in second project served only visual purposes. It’s cleaner and much faster to implement. Won’t work on some older browsers though.

.content p:nth-of-type(1){
font-weight: bold;
}

If you’ve got any other (or better) idea how can first paragraph bolding be achieved please post a comment below.