首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想在段落中找到一个单词的第一个字母的strpos

问我想在段落中找到一个单词的第一个字母的strpos
EN

Stack Overflow用户
提问于 2019-06-27 13:38:16
回答 3查看 105关注 0票数 0

若要查找段落中带有strpos()的单词的第一个字母,请执行以下操作。我试着提取段落中单词的所有第一个字母..

代码语言:javascript
复制
$paragraph = "A pangram or holoalphabetic win sentence is a sentence that contains every knight letter of the alphabet at least once.  
              The most famous pangram is probably the thirty-five-letter-long The quick brown fox jumps over the lazy dog,  
              which has been used to test typing equipment since at least the late Pangrams are an important tool for 
              testing typing equipment and compactly showing off every letter of price related etc";

$array = range('a','z');

// to find the first letter of each word in the paragraph 

$words = explode(" ", $paragraph);
$letters = "";
foreach ($words as $value) {
    $letters .= substr($value, 0, 1);
}

$countLetters = strtolower($letters);

====

如果我给出'p‘作为选定的字母表,那么我想要找到它的第一个和最后一个单词的字符串位置。

段落中的第一个单词with p= "pangram“-->查找p的字符串...

段落中的最后一个单词with p= "price“-->查找p的字符串...

例如..

代码语言:javascript
复制
output = {
            "p":{
                    "firstWordofFirstLetterPosition":2,
                    "firstWordofLastLetterPosition":"strpos of p in price"
                }
EN

回答 3

Stack Overflow用户

发布于 2019-06-27 14:27:43

这是你想要的吗?支持多字节字符串:

代码语言:javascript
复制
<?php

function firstWordsInParagraph(string $input, string $letter) {
    $lowercaseLetter = mb_strtolower($letter);

    // return only words having given letter
    $words = array_filter(explode(' ', $input), function ($word) use ($lowercaseLetter) {
        return mb_strpos(mb_strtolower($word), $lowercaseLetter) !== false;
    });

    // no matches found!
    if (empty($words)) {
        return [
            'firstWordofFirstLetterPosition' => null,
            'firstWordofLastLetterPosition' => null,
        ];
    }

    return [
        'firstWordofFirstLetterPosition' => mb_strpos(current($words), $lowercaseLetter),
        'firstWordofLastLetterPosition' => mb_strpos(end($words), $lowercaseLetter),
    ];
}

$input = 'kolorowa żółć w żniw pożodze';
$letter = 'ż';

print_r(firstWordsInParagraph($input, $letter));

例如:https://3v4l.org/7Go7i

返回:

代码语言:javascript
复制
Array (
   [firstWordofFirstLetterPosition] => 0
   [firstWordofLastLetterPosition] => 2
)
票数 0
EN

Stack Overflow用户

发布于 2019-06-27 15:09:28

如果您尝试使用正则表达式,则可以以更简单和可读性更强的方式将其存档

代码语言:javascript
复制
<?php

$paragraph = "A pangram or holoalphabetic win sentence is a sentence that contains every knight letter of the alphabet at least once.  
              The most famous pangram is probably the thirty-five-letter-long The quick brown fox jumps over the lazy dog,  
              which has been used to test typing equipment since at least the late Pangrams are an important tool for 
              testing typing equipment and compactly showing off every letter of price related etc";

$findMe = 'p';

function getPositionAndWordsFromParagraph($paragraph,$findMe){
    $output_array = [];
    preg_match_all('/\b['.$findMe.']/', $paragraph, $output_array, PREG_OFFSET_CAPTURE);
    $first = array_shift($output_array[0]);
    $last = array_pop($output_array[0]);
    return [
        $findMe => [
            'firstWordofFirstLetterPosition' => $first[1],
            'firstWordofLastLetterPosition' => $last[1]
        ]
    ];
}

print_r(getPositionAndWordsFromParagraph($paragraph,$findMe));
票数 -1
EN

Stack Overflow用户

发布于 2019-06-27 15:25:44

使用preg_match_all()

代码语言:javascript
复制
<?php
$paragraph = "A pangram or holoalphabetic win sentence is a sentence that contains every knight letter of the alphabet at least once.  
              The most famous pangram is probably the thirty-five-letter-long The quick brown fox jumps over the lazy dog,  
              which has been used to test typing equipment since at least the late Pangrams are an important tool for 
              testing typing equipment and compactly showing off every letter of price related etc";

function findFirstLastPositionOfWordsStartingWith($findMe, $string){
    $matches = [];
    preg_match_all('/\b('.$findMe.'.*)\b/i',$string, $matches, PREG_OFFSET_CAPTURE);
    $matches = $matches[1];
    return [
        "firstWordofFirstLetterPosition" => reset($matches)[1],
        "firstWordofLastLetterPosition" => end($matches)[1],
    ];
}

var_dump(findFirstLastPositionOfWordsStartingWith('p', $paragraph));
var_dump(findFirstLastPositionOfWordsStartingWith('a', $paragraph));

上面的代码将输出以下内容:

代码语言:javascript
复制
array(2) {
  ["firstWordofFirstLetterPosition"]=>
  int(2)
  ["firstWordofLastLetterPosition"]=>
  int(450)
}
array(2) {
  ["firstWordofFirstLetterPosition"]=>
  int(0)
  ["firstWordofLastLetterPosition"]=>
  int(408)
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56784560

复制
相关文章

相似问题

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