Reputation: 5132
I have a string in php with multiple periods. I am trying to figure out to replace each of the periods in the string into a bullet (<li>
) on a new line. Does anyone have advice on how to do this?
$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;
The above string should look like:
<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\'
Upvotes: 0
Views: 584
Reputation: 8191
Use str_replace:
$desc = '<li>' . str_replace('.','</li><li>', $desc) . '</li>';
Upvotes: 1
Reputation: 2477
Did you try
$bulletted = str_replace(".", "<br />•", $desc);
See http://php.net/manual/en/function.str-replace.php
Upvotes: 0
Reputation: 17434
As long as you're sure that every period can be replaced, what you want is implode()
and explode()
:
$desc = '<li>' . implode('</li><li>', explode('.', $desc)) . '</li>';
Upvotes: 4