访问PHP数组中的子字符串

时间:2017-07-23 16:31:33

标签: php python

假设我有一个这样的数组:

$check="Ninja Turtles";
$key=array("jack and jil","peter likes pan","Ninja Turtles");

我想查找关键字“Ninja Turtels' ARRAY中存在$ check变量,如果是这样,我想知道它存在于ARRAY的哪个INDEX中。我的上面的代码工作得非常好,但是如果我的$ check变量有一些额外的单词如下:

$check="Ninja blah bleh Turtles"

我的代码当时没有工作。我想忽略“bla ble&”我的字符串中的单词。

我设法用Python解决这个问题,因为我有多年的python经验,但我是php新手。

我的代码

foreach (array_values($key) as $i => $val) {
    $pos = strpos($check, $val);
    if ($pos === false) {
           echo "The string '$chechk' was not found in the ARRAY";
    } else {
            echo "Found '$check' at Postion: '$i'";


       }

  }

我的完整Python代码

check="Ninja bleh balah Turtles"
key=["jack and jil","peter likes pan","Ninja Turtles"]
for index, name in enumerate(KEY):
     if(name in phrase):
         print("Found", name +" Index is ", index)
         active="Found", name +" Index is ", index
         break
     else:
         newkey=name.split()
         newphrase=check.split()
         num = 2
         l = [i for i in newkey if i in newphrase]
         if len(l) >= num:
             print('Found', name +" "+ "Index is", index)
             active="Found", name +" Index is ", index
             break

4 个答案:

答案 0 :(得分:1)

这样的事情可以帮助你开始...... https://iconoun.com/demo/temp_elo.php



    clientService = ClientServiceCalls()

    #get all client IDs

    clientResponse = clientService.GetClientsByString('')
    clientList = clientResponse.Clients.Client

    clientVisitsDict = []

    for c in clientList:

        #Call get ClientVisits API on each Client ID

        clientResponseVisits = clientService.GetClientVisits(str(c.ID))

        if clientResponseVisits.Visits:
            visitsList = clientResponseVisits.Visits.Visit
            for v in visitsList:

                ### your code here




答案 1 :(得分:1)

我没有将匹配与数组匹配,而是使用preg_match进行相反的操作。

$check="Ninja blah bleh Turtles";
$key=array("jack and jil","peter likes pan","Ninja Turtles");

Foreach($key as $k => $val){
    $pattern = "/" . Implode("|", explode(" ",$val)) . "/";
    //Echo $pattern;
    If(preg_match($pattern, $check)) echo $k;
}

输出:

2 //as in $key[2] is where it matches.

https://3v4l.org/duYpU

爆炸" Ninja Turtles"排列并将它们放回到|之间(正则表达式) 然后添加/以完成正则表达式模式 检查此模式是否与$ check匹配 如果是真正的echo键值。

编辑;在做菜时我意识到$ pattern可以这样做。

$pattern = "/" . Str_replace(" ", "|", $val) ."/";

它只是用正则表达式替换空间,而不是爆炸和爆炸。可能没有太大的性能差异,但这是更正确的方法。

答案 2 :(得分:1)

类似的PHP代码是:

$check = "Ninja bleh balah Turtles";
$key = ["jack and jil", "peter likes pan", "Ninja Turtles"];

foreach($key as $index => $name) {
    if ($name === $check) {
        echo "Found $name Index is $index";
        return;
    } else {
        $newKey = explode(' ', $name);
        $newPhase = explode(' ', $check);
        $num = 2;
        $l = array_filter($newKey, function ($nkey) use ($newPhase) {
            return in_array($nkey, $newPhase);
        });
        if (count($l) >= $num) {
            echo "Found $name Index is $index";
            return;
        }
    }
}

答案 3 :(得分:1)

尝试使用tis:

$check = "Ninja bleh bleh Turtles";
$key = array("jack and jil","peter likes pan","Ninja Turtles");

foreach ($key as $k => $keyValue) {
    foreach (explode(' ', $check) as $valueCheck) {
        if (strstr($keyValue, $valueCheck)) {
            printf('[%s => %s]', $k, $keyValue);
            break;
        }
    }
}

结果:

[2 => Ninja Turtles]

但您可以使用array_filter()

$check = "Ninja bleh bleh Turtles";
$key = array("jack and jil","peter likes pan","Ninja Turtles");

$found = array_filter($key, function($value) use($check) {
    return array_filter(explode(' ', $check), function($valueKey) use($value) {
        return strstr($value, $valueKey);
    });
});

print_r($found);

结果:

Array
(
    [2] => Ninja Turtles
)

或使用preg_grep()和reg表达式:

$check = "Ninja bleh bleh Turtles";
$key = array("jack and jil","peter likes pan","Ninja Turtles");

$pattern = '/' . str_replace(' ', '|', $check) . '/';
$foundbyRegex = preg_grep($pattern, $key);

print_r($foundbyRegex);

结果:

Array
(
    [2] => Ninja Turtles
)