我想为所有preg特殊字符添加尾部斜杠。。例如,http://www.youtube.com/watch?v=i9c4PJTDljM
应转换为http':'/'/www'.youtube'.com'/watch'?v'=i9c4PJTDljM
我尝试了低于代码
echo preg_quote($url);
但它并没有在反斜杠中添加尾随斜杠。结果是这样的
http'://www'.youtube'.com/watch'?v'=i9c4PJTDljM
<?php
$content = 'http://www.youtube.com/watch?v=i9c4PJTDljM';
//With this pattern you found everything except 0-9a-zA-Z
$pattern = "/[_a-z0-9-]/i";
$new_content = '';
for($i = 0; $i < strlen($content); $i++) {
//if you found the 'special character' then add the '
if(!preg_match($pattern, $content[$i])) {
$new_content .= '''' . $content[$i];
} else {
//if there is no 'special character' then use the character
$new_content .= $content[$i];
}
}
print_r($new_content);
?>
输出:
http://www.youtube.com/watch''?v''=i9c4PJTDlj