Php 我想找一个词';s的第一个字母';在一个段落中的主题
在段落中使用Php 我想找一个词';s的第一个字母';在一个段落中的主题,php,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
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的strop
段落的最后一个字是p=“price”-->查找p的STRPO
例
这是你想要的吗?支持多字节字符串:
<?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));
如果您尝试使用正则表达式,则可以以更简单易读的方式存档它
<?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));
使用
巴拉吉现在检查我的答案。
Array (
[firstWordofFirstLetterPosition] => 0
[firstWordofLastLetterPosition] => 2
)
<?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));
<?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)
}