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

时间:2019-06-27 05:38:16

标签: php

在段落中以strpos()查找单词的第一个字母。我尝试通过获取段落中单词的所有首字母来进行尝试。

$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'作为选定的字母,那么我想找到它的第一个和最后一个单词的字符串位置。

段落中的第一个单词,其中p =“ pangram”->查找p的strpos ...

段落中最后一个带有p =“ price”的单词->找到p的strpos ...

例如。

output = {
            "p":{
                    "firstWordofFirstLetterPosition":2,
                    "firstWordofLastLetterPosition":"strpos of p in price"
                }

3 个答案:

答案 0 :(得分:0)

这是您想要的吗?具有多字节字符串支持:

<?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

返回:

Array (
   [firstWordofFirstLetterPosition] => 0
   [firstWordofLastLetterPosition] => 2
)

答案 1 :(得分:-1)

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

<?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));

答案 2 :(得分:-1)

使用preg_match_all()

<?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));

上面将输出以下内容:

array(2) {
  ["firstWordofFirstLetterPosition"]=>
  int(2)
  ["firstWordofLastLetterPosition"]=>
  int(450)
}
array(2) {
  ["firstWordofFirstLetterPosition"]=>
  int(0)
  ["firstWordofLastLetterPosition"]=>
  int(408)
}