我在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''
只要你确定每个周期都可以被替换,你想要的就是implode()
和explode()
:
$desc = '<li>' . implode('</li><li>', explode('.', $desc)) . '</li>';
使用str_replace:
$desc = '<li>' . str_replace('.','</li><li>', $desc) . '</li>';
http://ideone.com/Z62S5
你试过吗
$bulletted = str_replace(".", "<br />•", $desc);
请参阅http://php.net/manual/en/function.str-replace.php