我有这个如何使用preg_match在PHP中匹配特殊字符?
$content1="W've have come across";
$content2="W've have come across all the oceans but still we don't find anything";
我不得不去掉之间“我们所遇到的”和“什么”
我应该使用什么样的正则表达式时的内容1,内容2可以更改内容但它们可能包含撇号或特殊字符?
如何在有特殊字符时使用preg_match?
我有这个如何使用preg_match在PHP中匹配特殊字符?
$content1="W've have come across";
$content2="W've have come across all the oceans but still we don't find anything";
我不得不去掉之间“我们所遇到的”和“什么”
我应该使用什么样的正则表达式时的内容1,内容2可以更改内容但它们可能包含撇号或特殊字符?
如何在有特殊字符时使用preg_match?
我用的是这样的:
function escape_regexp($regexp)
{
$regex_chars = '^.[]$()|*+?{}' . "\\";
$regexp = addcslashes($regexp, $regex_chars);
return $regexp;
}
,然后如拨打:
preg_replace('/something' . escape_regexp($var1) .
'something else/', '', $string)
我没有使用preg_quote(),因为我一般正则表达式逃逸想要的功能,不只是PHP,而且还为MySQL。我想要一个不同的字符集。我不希望字符<和>被逃脱 - 我没有找到逃脱它们的理由。
没有正则表达式,我会使用这样的:
function weirdExtract($str, $start, $end){
$posa = strripos($str, $start);
$posb = strripos($str, $end);
if($posa === false || $posb === false) return '';
$startlen = strlen($start);
return substr($str, $posa + $startlen, $posb - $startlen);
}
像这样来使用:http://codepad.viper-7.com/BJmEGG
strripos()可以用相应的功能改变,如果情况是很重要的。
每个“$ content”是否总是以“我们碰到过”开始并以“任何”结尾?上面列出的'$ content1'的预期输出是什么? – sberry
为什么不使用'str_replace()'来替换已知的子串? – Tomalak
内容不以“我们遇到过”开头它可能包含特殊字符或不可以 str_replace - 是否有任何替换所有已知的特殊字符?因为有时内容可能以UTF- 8编码也是:( – Shan