Regex for replacing <p class="someClass"> with <h2> tag?
Asked Answered
S

4

6

I need to replace all:

<p class="someClass someOtherClass">content</p>

with

<h2 class="someClass someOtherClass">content</h2>

in a string of content. Basically i just want to replace the "p" with a "h2".

This is what i have so far:

/<p(.*?)class="(.*?)pageTitle(.*?)">(.*?)<\/p>/

That matches the entire <p> tag, but i'm not sure how i would go about replacing the <p> with <h2>

How would i go about doing this?

Sevastopol answered 8/11, 2012 at 8:14 Comment(7)
use preg_replace. php.net/manual/en/function.preg-replace.phpLeft
Well yes, i know that, but i don't know how to write the actual expression!Sevastopol
@Sevastopol maybe because this question is asked a lot of times (but in another expression) ? take a look at simplehtmldom.sourceforge.net You may not need regexp ...Unequivocal
Using a dom parser to replace one or two <p> tags on the page seems a bit "too much" when you can use a one-line solution with preg_replace(). Of course i searched before i created this question, but i didn't find anything that answered my question (with preg_replace)Sevastopol
@Sevastopol well you didn't also specify if you want to target a <p> tag with specific class or all <p> tags ...Unequivocal
My bad, should've been more clear!Sevastopol
You must backreference the things you want to replace and then replace them.Ceyx
S
14

The following should do what you want:

$str = '<p>test</p><p class="someClass someOtherClass">content</p>';

$newstr = preg_replace('/<p .*?class="(.*?someClass.*?)">(.*?)<\/p>/','<h2 class="$1">$2</h2>',$str);

echo $newstr;

The dot(.) matches all. The asterisk matches either 0 or any number of matches. Anything inside the parenthesis is a group. The $2 variable is a reference to the matched group. The number inside the curly brackets({1}) is a quantifier, which means match the prior group one time. That quantifier likely isn't needed, but it's there anyway and works fine. The backslash escapes any special characters. Lastly, the question mark makes the .* bit be non-greedy, since by default it is.

Spiker answered 8/11, 2012 at 8:40 Comment(7)
Almost! But the classes for the <p> tag might differ, i want to "copy" all classes from the <p> tag to the <h2> tag as well, i don't want to hardcode it. It that possible?Sevastopol
@Sevastopol It is. Just replace class="etc" with $1. I'll edit my above example to do that.Spiker
@Sevastopol Could you expand on 'differ'? Some more data in that regard would be preferable, such as the classes being in a different order.Spiker
As long as someClass exists, and it is a <p> tag, i want to make it a <h2> tag, but if it has other classes as well, i still want to make it a <h2> tag. So, <p class="someClass">content</p> will become <h2 class="someClass">content</h2> and <p class="class someClass class">content</p> will become <h2 class="class someClass class">content</h2>Sevastopol
@Sevastopol I've updated my answer. It will match the <p> tag as long as someClass exists in the class tag. If other classes are within that tag, it will copy those too.Spiker
@Spiker What if there were attributes mentioned in the <p> tag and you had to retain them exactly as they are and just replace the <p part of it? For example, how would you change <span style="padding: 10px;">Test</span> to <div style="padding: 10px;">Test</div> ?Tetragrammaton
@Denver Regex is not made for parsing html. A DOM parser should be used in that case, but the OP didn't seem to want more than what was given.Spiker
F
1

Do not do it better, but it will help :)

$text = '<p class="someClass someOtherClass">content</p>';
$output = str_replace( array('<p', '/p>'), array('<h2', '/h2>'), $text );
Flay answered 8/11, 2012 at 8:28 Comment(1)
The problem with that is that it replaces all <p> tags. I only want to replace the <p> tags with a specified class.Sevastopol
E
0

It will work :)

preg_replace('/<p .*?class="(.*?)">(.*?)<\/p>/','<h2 class="$1">$2</h2>',$value);
Eisegesis answered 13/6, 2013 at 12:33 Comment(1)
What if there were attributes mentioned in the <p> tag and you had to retain them exactly as they are and just replace the <p part of it? For example, how would you change <span style="padding: 10px;">Test</span> to <div style="padding: 10px;">Test</div> ?Tetragrammaton
S
0

Sorry, little late to the party. I used the regex from the answer:

$str = '<p>test</p><p class="someClass someOtherClass">content</p>';

$newstr = preg_replace('/<p .*?class="(.*?someClass.*?)">(.*?)<\/p>/','<h2 class="$1">$2</h2>',$str);

echo $newstr;

this approach has a problem when you have more p tags, like in a block of text: Here is how I hardened the regex to cover this situation:

$newstr = preg_replace('/<p [^<]*?class="([^<]*?someClass.*?)">(.*?)<\/p>/','<h2 class="$1">$2</h2>',$str);
Sustentation answered 5/9, 2016 at 21:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.