我在我网站上的产品中使用数据馈送,我对它们的格式化方式有疑问。我正试图将一个片段放在一起为我排序。
以下是Feed当前的显示方式: “这是产品描述之一。以下是我需要提取的功能但我需要帮助的方式”
我需要做的是取出字符串并使其显示如下:
这是产品说明。
我做了它找到了句号的最后一个实例(在“描述”之后)。然后,我将新字符串拆分为大写字母,并将其添加到列表中。
这是我现在的代码,但它不起作用,我正在努力解决它的问题。你能帮忙吗?
$x = "This is one of the product description. Here are the featuresThat i need to extractBut how i need help"
$pos = strrpos($x, '.')+1;
$x = substr($x, $pos). '.';
preg_match_all('/[A-Z][^A-Z]*/', $x, $pieces);
$x = print "<ul>";
foreach($pieces as $piece) {
$x .= print "<li>";
$x .= $piece;
$x .= print "</li>";
}
$x = print "</ul>";
return $x;
答案 0 :(得分:0)
使用preg_split
$x = "This is one of the product description. Here are the featuresThat i need to extractBut how i need help";
$pos = strrpos($x, '.')+1;
$x = trim( substr($x, $pos). '.' );
$pieces = preg_split('/(?=[A-Z])/', $x, -1, PREG_SPLIT_NO_EMPTY);
$y = "<ul>";
foreach($pieces as $piece) {
$y .= "<li>";
$y .= $piece;
$y .= "</li>";
}
$y .= "</ul>";
return $y;
答案 1 :(得分:0)
您可以使用单个正则表达式来提取这3个句子:
$x = "This is one of the product description. Here are the featuresThat i need to extractBut how i need help";
preg_match_all('/^.+\.\h*(*SKIP)(*F)|([A-Z].*?)(?=[A-Z]|$)/', $x, $m);
print_r($m[1]);
Array
(
[0] => Here are the features
[1] => That i need to extract
[2] => But how i need help
)
或者在<ul><li>
使用中格式化它们:
$y = "<ul>\n";
foreach($m[1] as $item) $y .= "<li>$item</li>\n";
$y .= "</ul>";
echo $y;
<ul>
<li>Here are the features</li>
<li>That i need to extract</li>
<li>But how i need help</li>
</ul>