sharataka
sharataka

Reputation: 5132

How to use php to replace periods with bullets (li)?

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

Answers (3)

Josh
Josh

Reputation: 8191

Use str_replace:

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

http://ideone.com/Z62S5

Upvotes: 1

Lord Loh.
Lord Loh.

Reputation: 2477

Did you try

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

See http://php.net/manual/en/function.str-replace.php

Upvotes: 0

Interrobang
Interrobang

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

Related Questions