我正在尝试写一些类似于BB代码功能的东西,它取代了例如。 “[itemThumb type = file itemId = 33]”与所选项目的缩略图。带功能切换错误(?)的preg_replace
对于这个我在uniText()使用的preg_replace:
function universeText($str){
$str = preg_replace("#\[itemThumb type=(.*)\ typeId=(.*)\]#", showChatThumb("$1","$2") , $str);
return $str;
}
因为showChatThumb的输出wasnt工作,我减少showChatThumb()来:
function showChatThumb($itemType, $itemId){
switch($itemType){
case 'file':
$return = "rofl";
break;
case 'folder':
$return = "lol";
break;
case 'link':
$return = "asd";
break;
return $return;
}
但开关()函数不知何故,变量$ itemId无法正常工作。当我在开关函数之前或之后定义$ return时,它将向右传递替换函数。 我看过那个开关有时候不能正常工作,所以我也试着用if,否则如果已经这样但它也不起作用。
但如果我把它写这样的,正确的价值也将被退回扔替换功能:
function showChatThumb($itemType, $itemId){
return $itemType;
}
我很认真地现在毫无章法,感谢每一个帮助
我怀疑你可以使用一个函数作为'preg_replace()' –
它的'$ replacement'参数它的作用,但不知何故被处理的变量ins ide函数愚弄 –
我期望函数被调用时使用空字符串作为参数,并且'preg_replace()'函数使用返回值(它将是'null')。你可以用'switch()'中的'default:'动作来检查它。像Sudhir建议的'preg_replace_callback()'似乎是一种更好的方法。 –