$str = preg_replace("'\(look: (.{1,80})\)'Ui",
             "(look: <a href=\"dict.php?process=word&q=\\1\">\\1</a>)",$str);

我想编码URL,但是我怎么做呢?
在PREGL替换中可以使用URLCONDEVER()函数吗?,就像那样,
$str = preg_replace("'\(look: (.{1,80})\)'Ui",
            "(look: <a href=\"dict.php?process=word&q=\\1\">\\1</a>)",$str);

你知道在preg_replace中编码url吗?

最佳答案

您可以使用preg_replace_callback,它允许您通过直接运行代码来生成替换字符串:

$str = preg_replace_callback(
    "'\(look: (.{1,80})\)'Ui",
    create_function(
        '$matches',
        'return \'(look: <a href="dict.php?process=word&q='.urlencode($matches[1]).'">'.
          $matches[1].'</a>)\';'
    ),
    $str);

如果你使用PHP>=5.3,你可以让上面的痛苦减少一点:
$str = preg_replace_callback(
    "'\(look: (.{1,80})\)'Ui",
    function($matches) {
        return "(look: <a href=\"dict.php?process=word&q=".urlencode($matches[1])."\">".
               $matches[1]."</a>)";
    },
    $str);

08-29 00:42