Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP正则表达式标题转换/负前瞻/toLowerCase_Php_Regex_Preg Replace_Lowercase_Negative Lookahead - Fatal编程技术网

PHP正则表达式标题转换/负前瞻/toLowerCase

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

我正在尝试将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 ]*?)<\/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]);