How to add attribute to first P tag using PHP regular expression?

Asked
Active3 hr before
Viewed126 times

7 Answers

attributeusingfirst
90%

Reading through the answers there are some that will work but all have drawbacks of either using an external parsing library or possibly matching tags other than the P tag or also matching its attributes.,You may find it easier and more reliable to use an HTML parser such as this one. HTML is notoriously difficult to parse reliably (technically, impossible) with regular expressions, and the parser will give you a very simple means to find the nodes you're interested in. The first page of the doc has a tab labelled "How to modify HTML elements".,Use an HTML parser. Indeed, regexp are not a good tool to do what you want to do. Since loading a whole HTML parser for just this purpose is overkill, you'd really better be using Javascript., Umm, am I missing something or could you just change the page template to hardcode the class attribute instead of hooking into get_posts – Gordon Feb 7 '10 at 13:26

Of course, in order to anwser the question, here is the best way I can't think of to make it happends with regexp. Though, I don't recommend it.

preg_replace('#(</h2>\s*<p[^>]*)>#im', '$1 class="first">', '<h2>Some header</h>
      <p>First paragraph of the post</p>
      <p>Second paragraph of the post</p> ');
load more v
88%

To get my cool styling on the first paragraph (it's one of those things that looks good only sparingly) I need to hook into the get_posts function to filter its output with a preg_replace.,WordPress spits posts in this format:,Simple enough and it works just as intended. Here's full WordPress code snippet to filter the first paragraph any time the_content() is called:,I can't use CSS3 meta-selectors because I need to support IE6, and I can't apply the :first-line meta-selector (this is one that IE6 supports) on the parent container because it would hit the H2 instead of the first P.

WordPress spits posts in this format:

<h2>Some header</h>
   <p>First paragraph of the post</p>
   <p>Second paragraph of the post</p>
   etc.
load more v
72%

strip_tags — Strip HTML and PHP tags from a string, This function does not modify any attributes on the tags that you allow using allowed_tags, including the style and onmouseover attributes that a mischievous user may abuse when posting text that will be shown to other users. , This function tries to return a string with all NULL bytes, HTML and PHP tags stripped from a given string. It uses the same tag stripping state machine as the fgetss() function. , This function should not be used to try to prevent XSS attacks. Use more appropriate functions like htmlspecialchars() or other means depending on the context of the output.

Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>
65%

The preg_match() function will tell you whether a string contains matches of a pattern.,The preg_match_all() function will tell you how many matches were found for a pattern in a string.,The preg_replace() function will replace all of the matches of the pattern in a string with another string.,Use a regular expression to do a case-insensitive search for "w3schools" in a string:

In the example above, / is the delimiter, w3schools is the pattern that is being searched for, and i is a modifier that makes the search case-insensitive.

/

In the example above, / is the delimiter, w3schools is the pattern that is being searched for, and i is a modifier that makes the search case-insensitive.

i
load more v
75%

$text = '<div class="test">
   <p>Clean <a href="#">text</a><br><b>Bold</b> text</p>
</div>';

$cleanText = strip_tags($text, ['p','b','i','br']);
40%

In some cases, you need to add attribute to HTML tag dynamically. Regular Expressions (Regex) makes it easy to replace or add attributes to HTML tags in PHP. You can easily add attribute to HTML tags using Regex and PHP.,The preg_replace() function searches string matches to the pattern (regular expression) and replaces with replacement. Use preg_replace() function with regular expression to add attribute to HTML tag using PHP.,You can add any attributes to HTML tags using preg_replace() function in PHP.,The following example code will add style attribute to HTML <a> tag.

The following example code will add style attribute to HTML <a> tag.

<?php$html = '<a href="https://www.codexworld.com" class="cw-link" title="CodexWorld">Visit CodexWorld</a>';$result = preg_replace('/(<a\b[^><]*)>/i', '$1 style="xxxx:yyyy;">', $html);
load more v
22%

No more than one element in the document may have the autofocus attribute. If put on more than one element, the first one with the attribute receives focus.,Note: An element with the autofocus attribute may gain focus before the DOMContentLoaded event is fired.,Try the language specified by a lang/xml:lang attribute on the element or any of its parents.,The form attribute lets you place an input anywhere in the document but have it included with a form elsewhere in the document.

<input type="button" name="button" value="Button" />
load more v

Other "attribute-using" queries related to "How to add attribute to first P tag using PHP regular expression?"