解析单词从字符串到数组

时间:2015-12-07 07:18:45

标签: php preg-match

我有一些这样的搜索结果

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

3 个答案:

答案 0 :(得分:0)

尝试这样的事情:

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

$results = array();

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

请注意,由于您的文字格式化方式,该数组将包含&#39; helleanKako&#39;而不是&#39;在那一个案例中。

答案 1 :(得分: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);
?>

答案 2 :(得分:0)

如果还不晚......:

NSDateComponents * components = [[NSDateComponents alloc] init];
    components.year = 2015;
    components.weekday=2;
    NSCalendar * calendar = [NSCalendar currentCalendar];
    NSDate * date = [calendar dateFromComponents:components];
    NSInteger length = [calendar rangeOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitYearForWeekOfYear forDate:date].length;
    NSLog(@"%ld",(long)length);

希望这是你所期望的