如何用php用子弹替换句号(li)?

时间:2012-02-01 03:52:44

标签: php

我在php中有一个包含多个句点的字符串。我试图找出将字符串中的每个句点替换为新行上的项目符号(<li>)。有没有人有关于如何做到这一点的建议?

$desc= "Petite Jeans. Petite Fit Guide,Tall Fit Guide Explore our 1969 denim boutique for fit facts, fabric notes, style tips, and more. Fabrication: Premium stretch denim. Wash: Faded dark blue. Hardware: Double button closure, zip fly. Features: Five pocket styling. Cut: Mid rise. Fit: Slim through the hip and thigh. Leg opening: Boot cut. Inseams: regular: 33\', tall: 37\', petite: 30\'";

echo $desc;

上面的字符串应如下所示:

<li>Petite Jeans. 
<li>Petite Fit Guide,Tall Fit Guide Explore our 1969 denim boutique for fit facts, fabric notes, style tips, and more. 
<li>Fabrication: Premium stretch denim. 
<li>Wash: Faded dark blue. 
<li>Hardware: Double button closure, zip fly. 
<li>Features: Five pocket styling. 
<li>Cut: Mid rise. 
<li>Fit: Slim through the hip and thigh. 
<li>Leg opening: Boot cut. 
<li>Inseams: regular: 33\', tall: 37\', petite: 30\'

3 个答案:

答案 0 :(得分:4)

只要您确定每个时段都可以替换,您想要的是implode()explode()

$desc = '<li>' . implode('</li><li>', explode('.', $desc)) . '</li>';

答案 1 :(得分:1)

使用str_replace

$desc = '<li>' . str_replace('.','</li><li>', $desc) . '</li>';

http://ideone.com/Z62S5

答案 2 :(得分:0)

你试过吗

$bulletted = str_replace(".", "<br />&bull;", $desc);

请参阅http://php.net/manual/en/function.str-replace.php