PHP: формат строки заменить и добавить символ - PullRequest
0 голосов
/ 23 сентября 2010

У меня есть этот код:

$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\">";

Я хочу удалить все теги html, удалить лишние пробелы и добавить двойную черту после последнего заглавного слова.Кроме того, после каждого тега </li> я буду добавлять точку.

Мне нужно отформатировать его перед тем, как поместить его в массив, поэтому на выходе должно быть:

$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!";

Заранее спасибо:)

1 Ответ

3 голосов
/ 23 сентября 2010

С учетом следующего:

$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\">";

Этот код должен помочь:

$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 ) ) );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...