若要查找段落中带有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‘作为选定的字母表,那么我想要找到它的第一个和最后一个单词的字符串位置。
段落中的第一个单词with p= "pangram“-->查找p的字符串...
段落中的最后一个单词with p= "price“-->查找p的字符串...
例如..
output = {
"p":{
"firstWordofFirstLetterPosition":2,
"firstWordofLastLetterPosition":"strpos of p in price"
}发布于 2019-06-27 14:27:43
这是你想要的吗?支持多字节字符串:
<?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));返回:
Array (
[firstWordofFirstLetterPosition] => 0
[firstWordofLastLetterPosition] => 2
)发布于 2019-06-27 15:09:28
如果您尝试使用正则表达式,则可以以更简单和可读性更强的方式将其存档
<?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));发布于 2019-06-27 15:25:44
<?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)
}https://stackoverflow.com/questions/56784560
复制相似问题