Regular expressions are an easy way to match or replace pieces of your content, but can be tricky if you are new to it. Here are a few examples to replace html tags.
Replace H tags
Replace all H2 to <span class=”header”>
$html = preg_replace('/<h2>(.*?)<\/h2>/', '<span class="header">$1</span>', $html);
String:
<h2>Header</h2><p>Some content</p>
<h2>Next header</h2><p>More content</p>
Regexp:
/<h2>(.*?)<\/h2>/
Match all content between <h2>
Output:
<span class=”header”>Header</span><p>Some content</p>
<span class=”header”>Next header</span><p>More content</p>
Match all h tags: h1,h2,h3,h4…
$html = preg_replace('/<h([0-9]{1})>(.*?)<\/h([0-9]{1})>/', '<span class="header$1">$2</span>', $html);
String:
<h1>Header</h1><p>Some content</p>
<h2>Next header</h2><p>More content</p>
<h5>Next header</h5><p>More content</p>
Regexp:
/<h([0-9]{1})>(.*?)<\/h([0-9]{1})>/
Match all content between <h followed by one diget >
Output:
<span class=”header1″>Header</span><p>Some content</p>
<span class=”header2″>Next header</span><p>More content</p>
<span class=”header5″>Next header</span><p>More content</p>
Replace FONT tags
Size
The font tag is an old tag and not used in modern html language, here’s a few examples how to replace them easily. size=”3″ is normal font size in most browsers so we calculate the percentage of any other size by dividing with 3 (SIZE/STANDARD SIZE) * 100
$html = preg_replace('/<font size="([\d])">(.*?)<\/font>/', '<span style="font-size:\'.round($1/3*100).\'%;">$2</span>', $html); eval("\$html='" . $html . "';" );
String:
Some <font size=”5″>big more</font> content and
some <font size=”2″>smaller content</font>
Regexp:
/<font size=”([\d])”>(.*?)<\/font>/
Match all content between <font size=” followed by one diget “> and </font>
Output:
Some <span style="font-size:167%;">big more</span> content and
some <span style="font-size:67%;">smaller content</span>
Color
$html = preg_replace('/<font color="(.*?)">(.*?)<\/font>/', '<span style="color:$1;">$2</span>', $html);
String:
Some <font color=”#ffffff”>white</font> content and
some <font color=”red”>red</font>
Regexp:
/<font color=”(.*?)”>(.*?)<\/font>/
Match all content between <font color=” followed by any “> and </font>
Output:
Some <span style="color:#ffffff;">white</span> content and
some <span style="color:red;">red</span>
Font face
$html = preg_replace('/<font face="(.*?)">(.*?)<\/font>/', '<span style="font-face:$1;">$2</span>', $html);
String:
Some <font face=”verdana”>verdana</font> content and
some <font face=”arial”>arial</font>
Regexp:
/<font face=”(.*?)”>(.*?)<\/font>/
Match all content between <font face=” followed by any “> and </font>
Output:
Some <span style="font-face:verdana;">verdana</span> content and
some <span style="font-face:arial;">arial</span>
Replace b with strong and i with em
$html = preg_replace('/<b>(.*?)<\/b>/', '<strong>$1</strong>', $html); $html = preg_replace('/<i>(.*?)<\/i>/', '<em>$1</em>', $html);
For Flash
Flash still use: font,i and b so when creating XML files with content you may have to convert your modern tagging to HTML 4 tags.
Interactive PHP
Learn interactive PHP click here to find out more