我正在使用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
)发布于 2015-03-06 15:36:21
若要查找单引号中的所有单词,请在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.发布于 2015-03-06 15:26:47
使用substr()检查字符串的起始/结束是否等于其他字符串,您正在搜索:
http://php.net/manual/en/function.substr.php
正如您可以注意到的,如果您想从字符串的末尾进行比较,请使用负$start值。
对于长度参数,使用strlen()获取字符串长度:
http://php.net/manual/en/function.strlen.php
发布于 2015-03-06 15:31:40
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);类似的函数可以用于后置词。
https://stackoverflow.com/questions/28901869
复制相似问题