Skip to content

Instantly share code, notes, and snippets.

@hilyjiang
Created May 7, 2013 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hilyjiang/5530752 to your computer and use it in GitHub Desktop.
Save hilyjiang/5530752 to your computer and use it in GitHub Desktop.
高亮XML代码的函数,CodeIgniter Helper
<?php
// 高亮XML代码
if ( ! function_exists('xml_highlight'))
{
function xml_highlight($s)
{
$s = htmlspecialchars($s);
$s = preg_replace("#&lt;([/]*?)(.*)([\s]*?)&gt;#sU", "<span style=\"color:#0000FF\">&lt;\\1\\2\\3&gt;</span>",$s);
$s = preg_replace("#&lt;([\?])(.*)([\?])&gt;#sU", "<span style=\"color:#800000\">&lt;\\1\\2\\3&gt;</span>",$s);
$s = preg_replace("#&lt;([^\s\?/=])(.*)([\[\s/]|&gt;)#iU", "&lt;<span style=\"color:#808000\">\\1\\2</span>\\3",$s);
$s = preg_replace("#&lt;([/])([^\s]*?)([\s\]]*?)&gt;#iU", "&lt;\\1<span style=\"color:#808000\">\\2</span>\\3&gt;",$s);
$s = preg_replace("#([^\s]*?)\=(&quot;|')(.*)(&quot;|')#isU", "<span style=\"color:#800080\">\\1</span>=<span style=\"color:#FF00FF\">\\2\\3\\4</span>",$s);
$s = preg_replace("#&lt;(.*)(\[)(.*)(\])&gt;#isU", "&lt;\\1<span style=\"color:#800080\">\\2\\3\\4</span>&gt;",$s);
return str_replace("\n", '<br style="height:0" />', $s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment