2015-12-07 32 views
0

我有一些searsh結果這樣解析的話弗朗字符串數組

$search = 'an'; 

而且有很多的文字這樣

$text = 'Anda goes wiwsh totot anaana here goes like this <p>helle</p><b>an</b>Kako isyaay an koliko ana an here <div>hello ana</div>'; 

你會看到有一個很多的話有一些字符串

我需要去那個字符串,並找到只包含搜索結果並放在數組中的單詞,當然我不需要HTML裏面,我的最終數組必須像這樣

$results = array ('Anda','anaana','an','an','ana','an','ana'); 
+0

不太確定了,但是你在尋找這樣的東西:https://3v4l.org/Z4Gka? – Rizier123

+0

我有PHP 5.2版,這不起作用? –

+0

我只需要一個數組,結果 –

回答

0

嘗試這樣:

$all_words = explode(' ', strip_tags($text)); 

$results = array(); 

foreach ($all_words as $word) { 
    if (substr($search, $word) !== FALSE) { 
     $results[] = $word; 
    } 
} 

注意,因爲這樣你的文本格式,陣列將包括「helleanKako」而不是「一個」在一個案例。找到匹配的字符時,如果它不是太晚

+0

這可能是問題:( –

+0

有沒有使用更復雜的標記器就沒有太多的工作可做。 – GPHemsley

0

Strrchr可以返回true,...

<?php 
$search = "an"; 
$text = 'Anda goes wiwsh totot anaana here goes like this <p>helle</p><b>an</b>Kako isyaay an koliko ana an here <div>hello ana</div>'; 
$all_words = explode(' ', $text); 

$results = array(); 

foreach ($all_words as $word) { 
if (strrchr($word,$search) == TRUE) { 
    $results[] = $word; 
} 
} 
var_dump($results); 
?> 
+0

如果還有更多的HTML,這是行不通的 –

0

...:

<?php 
$input = 'Anda goes wiwsh totot anaana here goes like this <p>helle</p><b>an</b>Kako isyaay an koliko ana an here <div>hello ana</div>'; 
$pattern = '/([^\s^>]*an[^\s^<]*)/i'; 

if (preg_match_all($pattern, $input, $matches)){ 
    print_r($matches); 
} 
?> 

Array 
(
    [0] => Array 
     (
      [0] => Anda 
      [1] => anaana 
      [2] => an 
      [3] => an 
      [4] => ana 
      [5] => an 
      [6] => ana 
     ) 

    [1] => Array 
     (
      [0] => Anda 
      [1] => anaana 
      [2] => an 
      [3] => an 
      [4] => ana 
      [5] => an 
      [6] => ana 
     ) 

) 

希望這是你所預期的