Php 在preg_replace中进行URL编码 $str=preg\u replace(“\(look:(.{1,80})\)”用户界面”, “(外观:)”,$str);

Php 在preg_replace中进行URL编码 $str=preg\u replace(“\(look:(.{1,80})\)”用户界面”, “(外观:)”,$str);,php,preg-replace,urlencode,Php,Preg Replace,Urlencode,我想对url进行编码,但我该怎么做呢 我可以在preg_replace?中使用urlencode()函数吗 $str = preg_replace("'\(look: (.{1,80})\)'Ui", "(look: <a href=\"dict.php?process=word&q=\\1\">\\1</a>)",$str); $str=preg\u replace(“\(look:(.{1,80})\)”用户界面”, “(外观:)”

我想对url进行编码,但我该怎么做呢

我可以在preg_replace?中使用urlencode()函数吗

$str = preg_replace("'\(look: (.{1,80})\)'Ui",
             "(look: <a href=\"dict.php?process=word&q=\\1\">\\1</a>)",$str);
$str=preg\u replace(“\(look:(.{1,80})\)”用户界面”,
“(外观:)”,$str);
您知道如何在preg_replace中编码url吗?

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

$str = preg_replace("'\(look: (.{1,80})\)'Ui",
            "(look: <a href=\"dict.php?process=word&q=\\1\">\\1</a>)",$str);
$str=preg\u replace\u回调(
“\(查找:(.{1,80})\)”用户界面,
创建函数(
“$matches”,
“return\”(看:)\”;“
),
$str);
如果您使用PHP>=5.3,则可以使上述操作稍微不那么痛苦:

$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);
$str=preg\u replace\u回调(
“\(查找:(.{1,80})\)”用户界面,
函数($matches){
return“(look:)”;
},
$str);

谢谢,我想你忘了将preg\u replace更改为preg\u replace\u callback第一个代码摘录缺少一些return语句中单引号的转义,应该是
'return\(看:)\;'非常感谢~!
$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);