2013-09-11 39 views
1

所以基本上,我只是試圖在字符串內搜索一個字符串。如何在PHP中查找字符串中的第一個匹配項?

雖然這比它稍微複雜一些。

我有三個小弦,onetwothree

而且我有一個大的字符串,它被保存爲一個變量,是相當長..幾段長的實際。

我需要創建一個函數,讓我看到哪個字符串先出現。

例如,一個字符串是這樣的:

Hello, testing testing one test some more two 

將返回one因爲它two之前發生。

又如:

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

將返回three因爲它既onetwo前發生。

有沒有人有任何建議或例子如何做到這一點?對PHP很新穎,不確定如何去做這件事。謝謝!

+1

使用上的每個 「小弦」'stripos',看看哪個位置是最低的? – Passerby

+0

你想要的位置或只是哪個字符串先來? –

回答

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 
+0

你的答案無縫工作,謝謝! – Fizzix

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個變量沒有太多的工作。

+0

但'$ threePosition'是'-1'以來的最低值嗎? – Fizzix

+0

您會檢查> 0。 – andrewb

+0

如果找不到針(不是-1),則strpos()返回false。您必須首先檢查每個變量是否爲假,然後比較那些不相互抵觸的變量。 –

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); 

?> 
+0

認爲你的答案可能是最可靠的,特別是在數組搜索的實現中。謝謝你的幫助! – Fizzix

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 

最佳的解決方案將減少超過$草垛迭代。你可以從字符串的開始處開始,每當你前進一個字符時,從當前位置開始尋找任何$針。只要你找到一個,賓果。

3

使用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')); 
+0

很好的答案,雖然我覺得有一個功能更容易閱讀和修改。下次肯定會使用這個,因爲它更簡單。謝謝! – Fizzix

+1

@fizzix我已經更新了我的答案,包括一個函數的樣子。 –

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 
相關問題