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
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 :)
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.