Php 将文本中的句子切分并存储在变量中
我在$content var中有一些文本,如下所示:Php 将文本中的句子切分并存储在变量中,php,arrays,Php,Arrays,我在$content var中有一些文本,如下所示: $content = $page_data->post_content; $sentence1 = 'first sentence of the text'; $sentence2 = 'second sentence of the text'; 我需要以某种方式分割内容并提取句子,将每个句子插入到它自己的var中。 大概是这样的: $content = $page_data->post_content; $sentence
$content = $page_data->post_content;
$sentence1 = 'first sentence of the text';
$sentence2 = 'second sentence of the text';
我需要以某种方式分割内容并提取句子,将每个句子插入到它自己的var中。
大概是这样的:
$content = $page_data->post_content;
$sentence1 = 'first sentence of the text';
$sentence2 = 'second sentence of the text';
等等
我该怎么做
PS
我在想这样的事情,但我需要每个句子都有一个循环:
$match = null;
preg_match('/(.*?[?\.!]{1,3})/', $content, $match);
$sentence1 = $match[1];
$sentence2 = $match[2];
Ty:)变量中是否需要它们?你不能使用数组吗
$SENTURE=explode(“.”,$page\u data->post\u内容)代码>
编辑:
如果需要变量:
$allSentence = explode(". ", $page_data->post_content);
foreach($allSentence as $key => $val)
{
${"sentence". $key} = $val;
}
在变量中是否需要它们?你不能使用数组吗
$SENTURE=explode(“.”,$page\u data->post\u内容)代码>
编辑:
如果需要变量:
$allSentence = explode(". ", $page_data->post_content);
foreach($allSentence as $key => $val)
{
${"sentence". $key} = $val;
}
假设每个句子以句号结尾,您可以使用explode
:
$content = $page_data->post_content;
$sentences = explode('.', $content);
现在,您的句子可以如下访问:
echo $sentences[0]; // 1st sentence
echo $sentences[1]; // 2nd sentence
echo $sentences[2]; // 3rd sentence
// and so on
请注意,您可以使用count
或sizeof
计算句子总数:
echo count($sentences);
为每个句子创建一个新的变量不是一个好主意,想象一下,您可能有一段很长的文本,这需要通过增加内存使用来创建那个数量的变量。您可以简单地使用数组索引$句子[0]
,$句子[1]
等等。假设每个句子都以句号结尾,您可以使用分解
:
$content = $page_data->post_content;
$sentences = explode('.', $content);
现在,您的句子可以如下访问:
echo $sentences[0]; // 1st sentence
echo $sentences[1]; // 2nd sentence
echo $sentences[2]; // 3rd sentence
// and so on
请注意,您可以使用count
或sizeof
计算句子总数:
echo count($sentences);
为每个句子创建一个新的变量不是一个好主意,想象一下,您可能有一段很长的文本,这需要通过增加内存使用来创建那个数量的变量。您只需使用数组索引$SENTANCES[0]
,$SENTANCES[1]
等等。不要使用单独命名的变量,如$sentence1
,$SENTENCES2
等。请使用数组
$sentences = explode('.', $page_data->post_content);
$sentences = preg_split('/[!?\.]\s?/', $content);
这将为您提供变量$page\u data->post\u content
中的“句子”数组,其中“句子”实际上表示句号之间的字符序列。如果使用句号来表示除句末以外的其他意思(例如“Mr.Watson”),那么这种逻辑就会出错
编辑:当然,正如您所建议的,您可以使用更复杂的逻辑来检测句子边界。您仍应使用数组,不要创建名称末尾带有数字的未知数量的变量。不要使用单独命名的变量,如$sentence1
,$sentence2
等。请使用数组
$sentences = explode('.', $page_data->post_content);
$sentences = preg_split('/[!?\.]\s?/', $content);
这将为您提供变量$page\u data->post\u content
中的“句子”数组,其中“句子”实际上表示句号之间的字符序列。如果使用句号来表示除句末以外的其他意思(例如“Mr.Watson”),那么这种逻辑就会出错
编辑:当然,正如您所建议的,您可以使用更复杂的逻辑来检测句子边界。您仍应使用数组,而不是创建名称末尾带有数字的未知数量的变量。假设一个句子是通过终止标点符号分隔的,可以选择后跟空格,您可以执行以下操作以获取数组中的句子
$sentences = explode('.', $page_data->post_content);
$sentences = preg_split('/[!?\.]\s?/', $content);
您可能还需要使用修剪任何其他空间
$sentences = array_map('trim', $sentences);
这样一来,$SESSIONS[0]
是第一个,$SESSIONS[1]
是第二个,依此类推。如果需要循环它们,可以使用foreach
:
foreach($sentences as $sentence) {
// Do something with $sentence...
}
假设一个句子是通过终止标点符号来分隔的,可以选择后跟空格,您可以执行以下操作以获得数组中的句子
$sentences = explode('.', $page_data->post_content);
$sentences = preg_split('/[!?\.]\s?/', $content);
您可能还需要使用修剪任何其他空间
$sentences = array_map('trim', $sentences);
这样一来,$SESSIONS[0]
是第一个,$SESSIONS[1]
是第二个,依此类推。如果需要循环它们,可以使用foreach
:
foreach($sentences as $sentence) {
// Do something with $sentence...
}
由点、逗号、感叹号分隔的单词;所以onCommas不会结束句子,所以你想用标点符号来分割你的文本?你是对的,不是逗号:)哈哈,愚蠢的meWell,这个信息可能在你的问题中,你不觉得吗?看看你得到的答案,你提供的信息越多,用点、逗号、感叹号分隔的单词就越准确;所以onCommas不会结束句子,所以你想用标点符号来分割你的文本?你是对的,不是逗号:)哈哈,愚蠢的meWell,这个信息可能在你的问题中,你不觉得吗?看看你得到的答案,你提供的信息越多,答案就越准确。我不能指责你回答了这个问题,但应该强调的是,使用这种代码是一个多么大的错误。我不能指责你回答了这个问题,但是,每次都应该强调使用这种代码是一个多么大的错误。您好,我非常喜欢您的解决方案,在这种情况下,您能让它工作吗?“我是第nr一句。后面是第nr二句”-在“.Ty:)@站长排序后没有空格,空格已成为可选。但是,请注意,如果文本中出现过类似于U.S.A
的标点缩写,那么这也会将其拆分。您好,我非常喜欢您的解决方案,您能在这种情况下使用它吗?“我是第nr一句。后面是第nr二句”-在“.Ty:)@站长排序后没有空格,空格已成为可选。但是,请注意,如果文本中出现过标点缩写,则也会将其拆分,如U.S.A
。