我试图在字符串内部的单词"in"answers"with"之间抓取一个单词。问题是我有另一个"with"的实例,它将扩展通配符。
下面是一个例子:
$item = 'This is a car in red with new tires and with a radio';
$pattern = = '/in (.*) with/i';
preg_match($pattern, $item, $matches);
的回报:
array(2) {
[0]=>
string(30) "in red with new tires and with"
[1]=>
string(22) "red with new tires and"
}
我希望$matches[1]是'red'
将样式改为:
$pattern = '/in ([^'s]*)(.*) with/i';
// Array ( [0] => in red with new tires and with [1] => red [2] => with new tires and )
其中([^'s]*)
表示-取除space
以外的所有符号