soaptray Posted November 21, 2006 Share Posted November 21, 2006 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 More sharing options...
sinisake Posted November 22, 2006 Share Posted November 22, 2006 [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 https://forums.phpfreaks.com/topic/27990-replace-tag-with-given-attribute-help/#findComment-128220 Share on other sites More sharing options...
Nicklas Posted November 23, 2006 Share Posted November 23, 2006 [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 https://forums.phpfreaks.com/topic/27990-replace-tag-with-given-attribute-help/#findComment-129149 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.