PHP从字符串中获取搜索词的数组 - php

是否有一种简单的方法来解析包含否定词的搜索词的字符串?

'this -that "the other thing" -"but not this" "-positive"' 

会变成

array(
  "positive" => array(
    "this",
    "the other thing",
    "-positive"
  ),
  "negative" => array(
    "that",
    "but not this"
  )
)

因此这些术语可用于搜索。

参考方案

下面的代码将解析您的查询字符串,并将其分为正面和负面搜索字词。

// parse the query string
$query = 'this -that "-that" "the other thing" -"but not this" ';
preg_match_all('/-*"[^"]+"|\S+/', $query, $matches);

// sort the terms
$terms = array(
    'positive' => array(),    
    'negative' => array(),
);
foreach ($matches[0] as $match) {
    if ('-' == $match[0]) {
        $terms['negative'][] = trim(ltrim($match, '-'), '"');
    } else {
        $terms['positive'][] = trim($match, '"');
    }
}

print_r($terms);

输出量

Array
(
    [positive] => Array
        (
            [0] => this
            [1] => -that
            [2] => the other thing
        )

    [negative] => Array
        (
            [0] => that
            [1] => but not this
        )
)

验证IBAN PHP - php

在设计新平台时,我们尝试集成IBAN编号。我们必须确保IBAN已经过验证,并且存储在数据库中的IBAN始终正确。那么验证数字的正确方法是什么? 参考方案 正如我在其他问题中解释的逻辑一样,我尝试自己创建一个函数。根据Wikipedia文章中解释的逻辑,在下面找到合适的功能。国家特定验证。它适合吗http://en.wikipedia.org/wiki/Int…

PHP:将数组值加在一起 - php

我相信这比标题听起来要难一些,但我可能完全错了。我有一个像这样的数组:[["londrina",15],["cascavel",34],["londrina",23],['tiradentes',34],['tiradentes',21]] 我希望能够采用通用…

PHP JQuery复选框 - php

我有以下片段。 var myData = { video: $("input[name='video[]']:checked").serialize(), sinopse: $("#sinopse").val(), dia: $("#dia").val(), quem: $(&#…

在xpath中选择多个条件 - php

我正在尝试使用来自高尔夫比赛的xml提要,以显示每个高尔夫球手在高尔夫球场上的位置。目前,我想展示符合两个条件的所有高尔夫球手(排在前25名,以及所有加拿大高尔夫球手)。这是xml提要的示例。<GolfDataFeed Type="Leaderboards" Timestamp="3/21/2012 9:18:09 PM&…

PHP:获取调用引用的数组名称 - php

假定以下函数并调用:function doSomething( &$someArray ) { // Do something to $someArray } $names=array("John", "Paul", "George", "Ringo"); doSomet…