PHP Regular Expression to convert html entities to their respective characters
I want to change
<lang class='brush:xhtml'>test</lang>
to
<pre class='brush:xhtml'>test</pre>
my code like that.
<?php
$content="<lang class='brush:xhtml'>test</lang>";
$pattern=array();
$replace=array();
$pattern[0]="/<lang class=([A-Za-z='\":])* </";
$replace[0]="<pre $1>";
$pattern[1]="/<lang>/";
$replace[1]="</pre>";
echo preg_replace($pattern, $replace,$content);
?>
but it'开发者_运维技巧s not working. How to change my code or something wrong in my code ?
There's quite a few problems:
- Pattern 0 has the
*
outside the group, so the group only matches one character - Pattern 0 doesn't include the
class=
in the group, and the replacement doesn't have it either, so there won't be aclass=
in the replaced string - Pattern 0 has a space after the class, but there isn't one in the content string
- Pattern 1 looks for
lang
instead of/lang
This will work:
$pattern[0]="/<lang (class=[A-Za-z='\":]*) ?>/";
$replace[0]="<pre $1>";
$pattern[1]="/<\/lang>/";
$replace[1]="</pre>";
How bout without regex? :)
<?php
$content="<lang class='brush:xhtml'>test</lang>";
$content = html_entity_decode($content);
$content = str_replace('lang','pre',$content);
echo $content;
?>
Using preg_replace is a lot faster than str_replace.
$str = preg_replace("/<lang class=([A-Za-z'\":]+)>(.*?)<\/lang>/", "<pre class=$1>$2</pre>", $str);
Execution time: 0.039815s [preg_replace] Time: 0.009518s (23.9%) [str_replace] Time: 0.030297s (76.1%) Test Comparison: [preg_replace] compared with.........str_replace 218.31% faster
So preg_replace is 218.31%
faster than the str_replace
method mentioned above. Each tested 1000 times.
精彩评论