Reputation: 5127
I have this code:
$array[] = "<b> FLYDANNA® HEADWRAP </b> <ul> <li type=\"circle\"><i> Sensational headwrap made from 6 pieces of fabric to fit the contours of your head perfectly </i> <li type=\"circle\"><i> Strong yet lightweight cotton wrap can be folded up and stored almost anywhere-;pocket, purse, backpack ... you name it! </i> <li type=\"circle\">";
I want to remove all the html tags, remove excess spaces and add a double dash after the last capital word. Also, after every </li>
tag I will add a period.
I need to format it before I put it in the array so the output must be:
$array[] = "FLYDANNA® HEADWRAP-- Sensational headwrap made from 6 pieces of fabric to fit the contours of your head perfectly. Strong yet lightweight cotton wrap can be folded up and stored almost anywhere-;pocket, purse, backpack ... you name it!";
Thanks in advance :)
Upvotes: 0
Views: 516
Reputation: 886
Given the following:
$str = "<b> FLYDANNA® HEADWRAP </b> <ul> <li type=\"circle\"><i> Sensational headwrap made from 6 pieces of fabric to fit the contours of your head perfectly </i> <li type=\"circle\"><i> Strong yet lightweight cotton wrap can be folded up and stored almost anywhere-;pocket, purse, backpack ... you name it! </i> <li type=\"circle\">";
This code should do the trick:
$str = strip_tags( $str );
$str = preg_replace( '/\s+/', ' ', $str );
$words = array_reverse( explode( ' ', $str ) );
foreach ( $words as $k => $s ) {
if ( preg_match( '/\b[A-Z]{2,}\b/', $s ) ) {
$words[$k] = $s . "--";
break;
}
}
$str = trim( join( ' ', array_reverse( $words ) ) );
Upvotes: 3