Php preg_replace_回调函数的正则表达式修复
字符串:Php preg_replace_回调函数的正则表达式修复,php,regex,preg-replace-callback,Php,Regex,Preg Replace Callback,字符串: $string = '{$string#anything#something this string will output default |ucfirst|strtoupper}'; PREG_REPLACE_回调代码(PHP): 输出(匹配项): 要求:代替{$string此字符串将输出默认值| ucfirst | strtoupper},我想使用{$string此字符串将输出默认值ucfirst | strtoupper}(注意:ucfirst前面的管道符号被删除) 重要提示
$string = '{$string#anything#something this string will output default |ucfirst|strtoupper}';
PREG_REPLACE_回调代码(PHP):
输出(匹配项):
要求:代替{$string此字符串将输出默认值| ucfirst | strtoupper}
,我想使用{$string此字符串将输出默认值ucfirst | strtoupper}
(注意:ucfirst
前面的管道符号被删除)
重要提示:输出(即$matches数组)应与上面打印的内容相同
非常感谢您的帮助,谢谢您的阅读。我尝试了您的代码来更改您的函数 代码
<pre>
<?php
$string = '{$string#anything#something this string will output default |ucfirst|strtoupper}'; http://www.magentocommerce.com/support/magento_core_api
preg_match('/\{\$?([^# ]+)\#?([^ ]+)? ?([^|]+)?[ \|]?([^\}]+)?\}/', $string, $matches);
var_dump($matches);
?>
</pre>
这符合你的期望吗?我们是否应该将“此字符串将输出”默认值替换为“您能听到我说话吗?”
您想在ucfirst之前删除到管道您想在字符串中执行此操作并随后更新reg exp吗?如果是,识别“ucfirst”字符串(第一个管道之前的最后一个字符串)的规则是什么
你想用preg_replace_回调做什么这个函数将为找到的每个motif调用一个函数(你的$matches参数)。。。这是你的主意吗
链接文档:
Array
(
[0] => {$string#anything#something can you hear me? |ucfirst|ucfirst|ucfirst|strtoupper}
[1] => string
[2] => anything#something
[3] => can you hear me?
[4] => ucfirst|strtoupper
)
<pre>
<?php
$string = '{$string#anything#something this string will output default |ucfirst|strtoupper}'; http://www.magentocommerce.com/support/magento_core_api
preg_match('/\{\$?([^# ]+)\#?([^ ]+)? ?([^|]+)?[ \|]?([^\}]+)?\}/', $string, $matches);
var_dump($matches);
?>
</pre>
array(5) {
[0]=>string(80) "{$string#anything#something this string will output default |ucfirst|strtoupper}"
[1]=>string(6) "string"
[2]=>string(18) "anything#something"
[3]=>string(32) "this string will output default "
http://php.net/manual/en/function.preg-replace-callback.php [4]=>string(18) "ucfirst|strtoupper"
}