1

The $content variable contains a string with HTML paragraph tags such as:

$content = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Aliquam mauris diam, gravida eget finibus varius.</p>";

How can I add a class "textStyle" to each paragraph using PHP regex so that it looks like this:

$content = "<p class="textStyle">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    <p class="textStyle">Aliquam mauris diam, gravida eget finibus varius.</p>";

Here is what I have tried so far:

<?php

$content = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    <p>Aliquam mauris diam, gravida eget finibus varius.</p>";

    $pattern = "/(<p)/";

    $replace = '<p class="textStyle"';

    preg_replace($pattern, $replace, $content);

?>
Mark
  • 121
  • 4
  • 13
  • possible duplicate of [How to add attribute to first P tag using PHP regular expression?](http://stackoverflow.com/questions/2216558/how-to-add-attribute-to-first-p-tag-using-php-regular-expression) – Saty May 23 '15 at 07:02
  • it doesnt work, probably i am missing something. – Mark May 23 '15 at 07:02
  • 4
    You should use DOM instead. – Ulver May 23 '15 at 07:03

4 Answers4

8

Regex (not advised):

If you must use regex, you can try something like this. It doesn't match the abilities of DOM manipulation (see the rest of my answer below), but if you are sure your <p> tags are consistent without any existing classes, then you can try this:

$pattern = "/<p([> ])/";
$replace = '<p class="textStyle"$1';
$content = preg_replace($pattern, $replace, $content);

This will match only <p> and <p so as to try to keep any other attributes you may have. However, it is difficult using regex to properly handle more complicated situations like <p id="row1" class="class1"> or q><p (some emoji face) .


DOM Manipulation:

For a much better solution (that doesn't use regex) you can instead do the following. The reason this is superior is because you can also replace class attributes, and automatically maintain other attributes. Plus, if there are HTML validation errors, the DOMDocument object will note these.

<?php

$content = "<p class=\"oldClass\">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    <p style=\"color:red\">Aliquam mauris diam, gravida eget finibus varius.</p>";

$dom = new DOMDocument();
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

// Evaluate P tags in HTML. This just shows
// that you can be more selective on your tags
$xpath = new DOMXPath($dom);
$tags = $xpath->evaluate("//p");

// Loop through all the found tags
foreach ($tags as $tag) {

    // Set class attribute        
    $tag->setAttribute("class", "textStyle");
}

// Save the HTML changes
$content = $dom->saveHTML();

Result:

<p class="textStyle">Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<p style="color:red" class="textStyle">Aliquam mauris diam, gravida eget finibus varius.</p></p>

Pay special attention to this bit:

loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

without this, you will end of up HTML wrapper tags.

Drakes
  • 23,254
  • 3
  • 51
  • 94
  • Never try to parse XML/HTML using regex. – Stephen May 23 '15 at 07:18
  • 1
    The OP asked a specific question with a given string. He didn't say if it was for a whole page or not. I agree with you in general, but for a small string (as I stated), he might be fine. – Drakes May 23 '15 at 07:24
  • Just FYI: in case p tag already has class attribute or the tag name is followed by a newline, this regex will not do a good job. – Wiktor Stribiżew May 23 '15 at 07:50
  • I've changed my vote now. (I still think you should remove any regex "solution" at all though) One thing though - wouldn't `foreach` on `getElementsByTagName` be simpler here? – Stephen May 23 '15 at 08:47
  • I do NOT think that the regex solution should be removed. The question is "Adding a class to each paragraph tag using PHP Regex [closed]" – Joe Feb 15 '23 at 07:03
3

Here is how you can do it with DOM:

<?php

$DOM = new DOMDocument();
$DOM->loadHTML("<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>\n<p>Aliquam mauris diam, gravida eget finibus varius.</p>");

$list = $DOM->getElementsByTagName('p');

foreach($list as $p){
    $p->setAttribute('class', 'textStyle');
  }

$DOM=$DOM->saveHTML();
echo $DOM;

See demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I understand you prefer a regex solution. Being a great fan of regex, I should note that changing XML or HTML structure will force you to switch to DOM. Sooner than later, I think. – Wiktor Stribiżew May 23 '15 at 07:54
0

It works but you have to do this:

$content = preg_replace($pattern, $replace, $content);
Wim Mostmans
  • 3,485
  • 1
  • 20
  • 20
0

Try this:

$pattern = "/<p/";
$replace = '<p class="textStyle"';
preg_replace($pattern, $replace, $content);
Sagar
  • 642
  • 3
  • 14