Jump to content

Replace Tag with given attribute... HELP!


soaptray

Recommended Posts

Hey there,

I will make this nice and short...

I have...
[b]"<p class="level2_heading" align="center">Heading Here</p>"[/b]

and I want to have php find and replace with...

[b]"<h3 align="center">Heading Here</p>"[/b]

Can anyone help me create a regex to find this, and replace the needed parts? I am stumped.
Link to comment
https://forums.phpfreaks.com/topic/27990-replace-tag-with-given-attribute-help/
Share on other sites

[code]
<?php
$input='This is some text: <a href=http://www.google.com>Click</a>
<p class="level2_heading" align="center">that is it</p>';
$patern="<p class=\"level2_heading\" align=\"center\">+.+</p>";
$replace='<h3 align="center">Heading Here</p>';
if(eregi($patern,$input,$match)){
$input=str_replace($match,$replace,$input);
echo $input;
}
?>
[/code]
I hope that's it :)
[CODE]<?php

$string = '<p class="level2_heading" align="center">Heading Here</p>';
echo preg_replace('~<p[^>]+>(.*?)<\/p>~is', '<h3 align="center">\\1</h3>', $string);

?>[/CODE]

Are you sure you want to keep the ending </p> tag and not change it to </h3> as in the example above ?
If you want to keep the </p> tag, replace preg_replace() with this:

[code=php:0]preg_replace('~<p[^>]+>(.*?)(<\/p>)~is', '<h3 align="center">\\1\\2', $string);[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.