-1

我有一个如下字符串:

例如:

$string = "A great way to start with your theme is to use the XML export file from the demo. Having this starting content makes it easier to see how some features are created. Download the demo content below and import it into your WordPress site to get a replica of the theme demo site.The second step is to navigate to the Tools options and click Import. Then on the bottom of the list.";

现在我需要将内容增加到 3 行,它应该是一个完整的句子。所以我想知道我该怎么做。PHP中是否有任何功能可以做到这一点?

4

2 回答 2

0

这应该从一些基本的正则表达式开始。您应该继续重新测试正则表达式的有效性,并在遇到特殊情况时对其进行调整。您无法真正防范任何事情(例如对您的问题的评论中所说的丝绸之火)。

<?php
$string = "A great way to start with your theme is to use the XML export file from the demo. Having this starting content makes it easier to see how some features are created. Download the demo content below and import it into your WordPress site to get a replica of the theme demo site.The second step is to navigate to the Tools options and click Import. Then on the bottom of the list.";

$result = preg_match_all("/(?:\w+\s)+(?:\w+\.\s|\w+\.[a-z](?:\w+\s)+(?:\w+\.)+)/", $string, $matches);

echo '<pre>';
print_r($matches);
echo '</pre>';
?>

问题在于您的字符串,您永远不知道代码逻辑中的“lastword.Wordofnewline”是什么。上面的正则表达式本身实际上是相同的。这将只匹配其中一种情况,就像这个字符串 wherereplica变成repli.ca

从您的主题开始的一个好方法是使用演示中的 XML 导出文件。拥有此起始内容可以更轻松地查看某些功能是如何创建的。下载下面的演示内容并将其导入您的 WordPress 站点,以获取主题演示站点的 repli.ca。第二步是导航到“工具”选项并单击“导入”。然后在列表的底部。

您确实必须根据大量测试自定义正则表达式,然后才能使其正常工作。如果可能(如果是这种情况),您应该让您的用户(作者)验证您的代码自动编辑的输出:)。

于 2013-02-20T10:34:28.730 回答
0

您需要格式化字符串,使其具有 /n 或 . 在每个句子之后,您可以拆分字符串(php 爆炸功能)。任何奇怪的东西,比如 49.99 英镑或“博士”。john Smith' 会破坏算法。

于 2013-02-20T12:14:49.697 回答