1

所以基本上,我只是想在一个字符串中搜索一个字符串。

虽然,它比这有点棘手。

我有三个小字符串one,,,,twothree

而且我有一个大字符串,它被保存为一个变量并且很长..实际上有几段长。

我需要创建一个函数,让我查看哪个字符串首先出现。

例如,这样的字符串:

Hello, testing testing one test some more two

将返回one,因为它之前发生过two

另一个例子:

Test a paragraph three and testing some more one test test two

将返回three,因为它发生在one和之前two

有人对如何做到这一点有任何建议或例子吗?对 PHP 来说非常新,不知道如何去做。谢谢!

4

6 回答 6

4

preg_match()与一个简单的交替使用:

if (preg_match('/one|two|three/', $string, $matches)) {
    echo $matches[0];
}

结果:

Hello, testing testing one test some more two
-> one

Test a paragraph three and testing some more one test test two
-> three

如果你也需要这个位置,你可以添加一个标志:

if (preg_match('/one|two|three/', $string, $matches, PREG_OFFSET_CAPTURE)) {
    echo 'Found ' . $matches[0][0] . ' @ ' . $matches[0][1];
}

作为一个函数:

function findFirst($string, array $needles)
{
    $pattern = '/' . join('|', array_map(function($str) {
        return preg_quote($str, '/');
    }, $needles)) . '/';

    if (preg_match($pattern, $string, $matches)) {
        return $matches[0];
    } else {
        return false;
    }
}

要使用:

echo findFirst($string, array('one', 'two', 'three'));
于 2013-09-11T03:05:21.283 回答
1

http://www.php.net/manual/en/function.strpos.php

就像

$longString = "Hello, testing testing one test some more two";
$onePosition = strpos($longString, "one");
$twoPosition = strpos($longString, "two");
$threePosition = strpos($longString, "three");

$onePosition; // 23
$twoPosition; // 42
$threePosition; // -1

然后你只需比较每个变量以找到最低的。笨重,但对于 3 个变量来说工作量不大。

于 2013-09-11T02:58:15.887 回答
1

这应该有效:

<?php

    $arrWords = array("one", "two", "three");
    $strInput = "Test a paragraph three and testing some more one test test two";

    function getFirstOccurrence($strInput, $arrWords) {
        $arrInput = explode(" ", $strInput);
        foreach($arrInput as $strInput) {
            if(in_array($strInput, $arrWords)) {
                return $strInput;
            }
        }
        return null;
    }

    print "First word is: " . getFirstOccurrence($strInput, $arrWords);

?>
于 2013-09-11T03:01:07.303 回答
1

这是一个示例算法:

function getFirstWord(array $needles, $haystack) {
    $best = null; //the best position
    $first = null; //the first word

    foreach($needles as $needle) {
        $pos = strpos($haystack, $needle);
        if($pos !== false) {
            if($best === null || $pos < $best) {
                $best = $pos;
                $first = $needle;
            }
        }
    }

    //returns the first word, or null if none of $needles found
    return $first;
}

$needles = array('one', 'two', 'three');
echo getFirstWord($needles, 'Hello, testing testing one test some more two'); // one
echo getFirstWord($needles, 'Test a paragraph three and testing some more one test test two'); // three

最佳解决方案将最小化 $haystack 上的迭代。您可以从字符串的开头开始,每次前进一个字符时,查找从当前位置开始的任何 $needles。一旦你找到一个,宾果游戏。

于 2013-09-11T03:01:23.800 回答
1

尝试这样的事情:

$string = 'Hello, testing testing one test some more two';
$words = Array("one", "two", "three");
$low_pos = strlen($string);
$first = '';
foreach($words as $word)
{
    $pos = strpos($string, $word);
    echo "Found ".$word." at ".$pos."<br />";
    if($pos !== false && $pos < $low_pos)
    {
        $low_pos = $pos;
        $first = $word;
    }
}

echo $string."<br />";
echo "FIRST: ".$first;

输出:

Found one at 23
Found two at 42
Found three at 
Hello, testing testing one test some more two
FIRST: one
于 2013-09-11T03:03:15.777 回答
0

如果你想变得花哨,你可以使用带有 array_map、array_filter、array_search 和 min 的闭包。

function whichFirst(array $list, $string){
    // get a list of the positions of each word
    $positions = array_map(function($val) use ($string){
                                return strpos($string, $val);
                            }, $list);
    // remove all of the unfound words (where strpos returns false)
    $positions = array_filter($positions, function ($x){ return $x !== false; });

    // get the value with the key matching the lowest position.
    $key = array_search(min($positions), $positions);

    return $list[$key];
}

例子:

$str = "Hello, testing testing one test some more two";
$list = ["one","two","three"];

echo whichFirst($list, $str);
// outputs one
于 2013-09-11T03:16:16.490 回答