2010-09-22 30 views
0

我有這樣的代碼:PHP:字符串格式的更換和添加​​字符

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

感謝提前:)

回答

3

考慮以下幾點:

$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))); 
+0

謝謝先生pnomolos快速回復! :D乾杯:)我現在就試試 – marknt15 2010-09-23 00:13:48