我想将整个文本字符串转换为大写,但在有角度的括号内写入的文本除外。
我还想删除趋势支架。
我制作了以下代码,但反之亦然:
$text='this PART should be CONVERTED to uppercase [This PART should not BE changed] this part should also be CONVERTED to uppercase [THIS part should also not be CHANGED] etc..';
$text = preg_replace_callback(
"(\[(.*?)\])is",
function($m) {
return strtoupper($m[1]);
},
$text);
echo $text;
答案 0 :(得分:2)
您可以使用PCRE动词SKIP
和FAIL
跳过这些匹配。您可以在此处详细了解这些内容,http://www.rexegg.com/regex-best-trick.html。
$text='this PART should be CONVERTED to uppercase [This PART should not BE changed] this part should also be CONVERTED to uppercase [THIS part should also not be CHANGED] etc..';
$text = preg_replace_callback(
"/\[(.*?)\](*SKIP)(*FAIL)|\w+/is",
function($m) {
return strtoupper($m[0]);
},
$text);
echo $text;
正则表达式演示:https://regex101.com/r/nwG2wW/4/
PHP演示:https://3v4l.org/rkFUGd
答案 1 :(得分:0)
Match a single character not present in the list below [^][]++
++ Quantifier — Matches between one and unlimited times, as many times as possible, without giving back (possessive)
][ matches a single character in the list ][ (case sensitive)
Positive Lookahead (?=\[|$)
Assert that the Regex below matches
1st Alternative \[
\[ matches the character [ literally (case sensitive)
2nd Alternative $
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)