PHP正则表达式标题转换/负前瞻/toLowerCase
我正在尝试将html页面中的一些标题转换为PHP正则表达式标题转换/负前瞻/toLowerCase,php,regex,preg-replace,lowercase,negative-lookahead,Php,Regex,Preg Replace,Lowercase,Negative Lookahead,我正在尝试将html页面中的一些标题转换为。模式很简单 <?php $test = "<p><strong>THIS IS A TEST</strong></p><div>And this is Random STUFF</div><p><strong>CP</strong></p>"; $pattern = "/<p><strong>([A-Z
。模式很简单
<?php
$test = "<p><strong>THIS IS A TEST</strong></p><div>And this is Random STUFF</div><p><strong>CP</strong></p>";
$pattern = "/<p><strong>([A-Z ]*?)<\/strong><\/p>/";
$replacement = "<h2>$1</h2>";
$test = preg_replace($pattern, $replacement, $test);
?>
试试看
您可以创建一个自定义函数,在每次匹配时调用该函数。在这个函数中,您可以决定a)不替换CP,b)不将$1放在第一位
希望这对您有所帮助,祝您好运。不替换CP的模式:
/((?!CP)[A-Z]*?)/
对于数据操作,请使用上面提到的preg\u replace\u callback
。或者只需检查$match[1]=“CP”
和返回“CP”
$pattern = "/<p><strong>([A-ZÀ-ÿ0-9 ']{3,}?)<\/strong><\/p>/";
$replacement = "<h2>$1</h2>";
$test[$i] = preg_replace_callback($pattern, create_function('$matches', 'return "<h2>".ucfirst(mb_strtolower($matches[1]))."</h2>";'), $test[$i]);