我有绳子
'OR "law studies"~2 AND here also NOT uni* "south West" word NOT this *eng'我想使用preg_replace_callback()来处理所有的进程--所有(AND|OR|NOT)之间的单词--我正在为regex模式而挣扎。你能建议一个符合标准的正则表达式吗?我想最后的结果是
或 “法学研究”~2 和 这里也是 不 uni*“西南”字 不 这个*英语
或“法律研究”~2而这里也不统一*“西南”这个词不是这个*我试过一切都没有用
提前谢谢你
发布于 2014-03-31 18:06:41
有几种方法可以解决这个问题。首先,我将使用preg_replace_callback来解决这个问题,因为这是您在问题中特别要求的:
$string = 'OR "law studies"~2 AND here also NOT uni* "south West" word NOT this *eng';
$string = preg_replace_callback('~(.*?)(?:OR|AND|NOT|$)~', 'callback_function', $string);
print $string;
function callback_function ($m) {
$return_string = preg_replace('/e/', '<b><font color=red>e</font></b>', $m[1]);
return $return_string;
}这将用粗体和红色突出显示字符串中的所有“e”。
还有一个类似的函数,您可以使用它来完成相同的事情:preg_split。这样做是根据正则表达式将字符串拆分为数组。下面是一些代码来演示:
$string = 'OR "law studies"~2 AND here also NOT uni* "south West" word NOT this *eng';
$keywords = preg_split("/(OR|AND|NOT)/", $string, -1, PREG_SPLIT_NO_EMPTY); // NO EMPTY LINES$keywords将包含以下内容:
Array
(
[0] => "law studies"~2
[1] => here also
[2] => uni* "south West" word
[3] => this *eng
)因此,您可以循环遍历数组的每一项,并在字符串中进行替换。
https://stackoverflow.com/questions/22687840
复制相似问题