views:

31

answers:

1

Hi,

I have this code:

$array[] = "<b>  FLYDANNA&reg; 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&reg; 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 :)

+2  A: 

Given the following:

$str = "<b>  FLYDANNA&reg; 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 ) ) );
pnomolos
Thanks sir pnomolos for the quick reply! :D Cheers :) I will try it now
marknt15