preg_split returns 非预期数字
preg_split returns a non expected number
我的第一个问题,所以请耐心等待我...
我有这个字符串:
"Create a script which will take a string and analyse it to produce
the following stats. Spicy jalapeno bacon ipsum dolor amet kevin
buffalo frankfurter, sausage jerky pork belly bacon. Doner sausage
alcatra short loin, drumstick ham tenderloin shank bresaola porchetta
meatball jowl ribeye ground round. Ribeye pork loin filet mignon,
biltong shoulder short ribs tongue ball tip drumstick ground round
rump pork chop. Kielbasa bacon strip steak andouille ham short ribs
short loin venison frankfurter spare ribs doner corned beef."
我用过这个功能:
$sentenceArray = preg_split('/[.]+/', $string);
foreach ($sentenceArray as $sentence) {
$numberOfWords = str_word_count($sentence);
echo '<br>' . $numberOfWords . '<br>';
}
我明白了:
16
14
16
18
17
0
我期待 16、14、16、18、17,但我不明白 0 是从哪里来的?!
非常感谢!
实际上,您需要排除最后一个不包含任何内容的字符串,这就是它在其中返回 0 个单词的原因。您可以使用以下代码。
<?php
$string = "Create a script which will take a string and analyse it to produce the following stats. Spicy jalapeno bacon ipsum dolor amet kevin buffalo frankfurter, sausage jerky pork belly bacon. Doner sausage alcatra short loin, drumstick ham tenderloin shank bresaola porchetta meatball jowl ribeye ground round. Ribeye pork loin filet mignon, biltong shoulder short ribs tongue ball tip drumstick ground round rump pork chop. Kielbasa bacon strip steak andouille ham short ribs short loin venison frankfurter spare ribs doner corned beef.";
$sentenceArray = explode(".", $string);
foreach ($sentenceArray as $sentence)
{
if ($sentence != "")
{
$numberOfWords = str_word_count($sentence);
echo '<br>' . trim($numberOfWords) . '<br>';
}
}
?>
发生这种情况是因为您的输入字符串以一个点结尾,并且 preg_split
也会在该点拆分,生成其后的字符串作为拆分,即空字符串。
改用preg_match_all
。此函数会将结果数组作为参数提供,而不是作为 return 值,并且该数组将被包装。也一样:
preg_match_all('/[^.]+/', $string, $sentenceArray);
foreach ($sentenceArray[0] as $sentence) {
$numberOfWords = str_word_count($sentence);
echo '<br>' . $numberOfWords . '<br>';
}
只需删除该句子中的最后一个点。
preg_split()
在句号处拆分,包括最后一个,这导致数组中返回空字符串。
有一个标志可以用来移除空元素
$sentenceArray = preg_split('/[.]+/', $string, -1, PREG_SPLIT_NO_EMPTY);
有关详细信息,请参阅 https://www.php.net/manual/en/function.preg-split.php
我的第一个问题,所以请耐心等待我...
我有这个字符串:
"Create a script which will take a string and analyse it to produce the following stats. Spicy jalapeno bacon ipsum dolor amet kevin buffalo frankfurter, sausage jerky pork belly bacon. Doner sausage alcatra short loin, drumstick ham tenderloin shank bresaola porchetta meatball jowl ribeye ground round. Ribeye pork loin filet mignon, biltong shoulder short ribs tongue ball tip drumstick ground round rump pork chop. Kielbasa bacon strip steak andouille ham short ribs short loin venison frankfurter spare ribs doner corned beef."
我用过这个功能:
$sentenceArray = preg_split('/[.]+/', $string);
foreach ($sentenceArray as $sentence) {
$numberOfWords = str_word_count($sentence);
echo '<br>' . $numberOfWords . '<br>';
}
我明白了:
16
14
16
18
17
0
我期待 16、14、16、18、17,但我不明白 0 是从哪里来的?! 非常感谢!
实际上,您需要排除最后一个不包含任何内容的字符串,这就是它在其中返回 0 个单词的原因。您可以使用以下代码。
<?php
$string = "Create a script which will take a string and analyse it to produce the following stats. Spicy jalapeno bacon ipsum dolor amet kevin buffalo frankfurter, sausage jerky pork belly bacon. Doner sausage alcatra short loin, drumstick ham tenderloin shank bresaola porchetta meatball jowl ribeye ground round. Ribeye pork loin filet mignon, biltong shoulder short ribs tongue ball tip drumstick ground round rump pork chop. Kielbasa bacon strip steak andouille ham short ribs short loin venison frankfurter spare ribs doner corned beef.";
$sentenceArray = explode(".", $string);
foreach ($sentenceArray as $sentence)
{
if ($sentence != "")
{
$numberOfWords = str_word_count($sentence);
echo '<br>' . trim($numberOfWords) . '<br>';
}
}
?>
发生这种情况是因为您的输入字符串以一个点结尾,并且 preg_split
也会在该点拆分,生成其后的字符串作为拆分,即空字符串。
改用preg_match_all
。此函数会将结果数组作为参数提供,而不是作为 return 值,并且该数组将被包装。也一样:
preg_match_all('/[^.]+/', $string, $sentenceArray);
foreach ($sentenceArray[0] as $sentence) {
$numberOfWords = str_word_count($sentence);
echo '<br>' . $numberOfWords . '<br>';
}
只需删除该句子中的最后一个点。
preg_split()
在句号处拆分,包括最后一个,这导致数组中返回空字符串。
有一个标志可以用来移除空元素
$sentenceArray = preg_split('/[.]+/', $string, -1, PREG_SPLIT_NO_EMPTY);
有关详细信息,请参阅 https://www.php.net/manual/en/function.preg-split.php