0

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 :)

1 Answer 1

3

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 ) ) );
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks sir pnomolos for the quick reply! :D Cheers :) I will try it now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.