如何使用php在字符串中找到以特定字母或元素开头的单词

时间:2015-03-06 15:20:59

标签: php

使用str_word_count()来计算单词出现在文本中的次数,但我真正想要的是只计算以'[word here]'开头和结尾的特定单词

$text = "Degree binb 'Information-Systems', 'Computer-Science' , or other KM-relevant field required; graduate degree preferred.";

$words = str_word_count($text, 1); 
$frequency = array_count_values($words);
arsort($frequency);
echo '<pre>';
print_r($frequency);
echo '</pre>';

输出:

Array
(
    [required] => 1
    [field] => 1
    [graduate] => 1
    [degree] => 1
    [preferred] => 1
    [KM-relevant] => 1
    [other] => 1
    [binb] => 1
    ['Information-Systems'] => 1
    ['Computer-Science'] => 1
    [or] => 1
    [Degree] => 1
)

3 个答案:

答案 0 :(得分:1)

要查找单引号中的所有单词,请使用带preg_match_all()的正则表达式:

$text = "Degree binb 'Information-Systems', 'Computer-Science' , or other KM-relevant field required; graduate degree preferred.";

preg_match_all("/'([^']+)'/", $text, $matches);

var_dump($matches[1]);
echo "Found " . count($matches[1]) . " matches." . PHP_EOL;

这将输出:

array(2) {
  [0] =>
  string(19) "Information-Systems"
  [1] =>
  string(16) "Computer-Science"
}
Found 2 matches.

答案 1 :(得分:0)

使用substr()检查字符串的开始/结尾是否等于其他字符串,您正在搜索:

http://php.net/manual/en/function.substr.php

正如你可以注意到的那样,如果你想从字符串末尾比较使用负$ start值。

对于长度参数,使用strlen()来获取字符串长度:

http://php.net/manual/en/function.strlen.php

答案 2 :(得分:0)

PHP伪代码示例:

function find_words_prefix($text, $prefix)
{
  $words = array_filter(explode(' ', $text), 'strlen'); // get only non-emoty words
  $prefixed_words = array();
  $prefix_len = strlen($prefix);
 foreach ($words as $word)
{
if (strlen($word) < $prefix_len) continue; // no use testing this word as it is smaller than prefix
if ( 0 === strpos($word, $prefix) ) $prefixed_words[] = $word;
}
return $prefixed_words;
}

像这样使用:

$prefixed_words = find_words_prefix("Degree binb 'Information-Systems', 'Computer-Science' , or other KM-relevant field required; graduate degree preferred.", "D");

print_r($prefixed_words);

类似的功能可用于后缀词

相关问题