首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用php在字符串中找到以特定leter或元素开头的单词?

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

Stack Overflow用户
提问于 2015-03-06 15:20:58
回答 3查看 99关注 0票数 1

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

代码语言:javascript
复制
$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>';

输出:

代码语言:javascript
复制
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
)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-03-06 15:36:21

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

代码语言:javascript
复制
$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;

这将产生以下结果:

代码语言:javascript
复制
array(2) {
  [0] =>
  string(19) "Information-Systems"
  [1] =>
  string(16) "Computer-Science"
}
Found 2 matches.
票数 1
EN

Stack Overflow用户

发布于 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

票数 0
EN

Stack Overflow用户

发布于 2015-03-06 15:31:40

PHP伪代码示例:

代码语言:javascript
复制
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;
}

像这样使用:

代码语言:javascript
复制
$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);

类似的函数可以用于后置词。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28901869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档