2014-09-26 46 views
0

我試圖如下采取一個輸入字符串:意外結果用PHP函數格式字符串

示例輸入

<p> 
    In spite of how delightfully smooth the full-grain leather may be, 
    it still provides quite a bit of durability to the upper of this 
    Crush by Durango women&#39;s Western boot. Everything about this 
    pull-on boot is stunning; you have the floral embroidery, the pop 
    of metallic and a stylish x toe. To make these 12&rdquo; floral 
    boots ease to get on, Durango added two very sturdy pull straps 
    next to the boot&rsquo;s opening.</p> 
<ul> 
    <li> 
     2 1/4&quot; heel</li> 
    <li> 
     Composition rubber outsole with vintage finish</li> 
    <li> 
     Cushion Flex insole</li> 
</ul> 

併產生以下輸出:

輸出字符串

In spite of how delightfully smooth the full-grain leather may be, 
it still provides quite a bit of durability to the upper of this Crush by 
Durango women's Western boot. Everything about this pull-on boot is stunning; 
you have the floral embroidery, the pop of metallic and a stylish x toe. 
To make these 12」 floral boots ease to get on, Durango added two very sturdy 
pull straps next to the boot’s opening. 

2 1/4" heel 
Composition rubber outsole with vintage finish 
Cushion Flex insole 

我有以下功能:

功能

function cleanString($str) 
{ 
    $content = ''; 

    foreach(preg_split("/((\r?\n)|(\r\n?))/", strip_tags(trim($str))) as $line) { 
    $content .= " " . trim($line) . PHP_EOL; 
    } 

    return $content; 
} 

在這個函數返回In spite of how delightfully smooth the full-grain leather may be和修剪字符串的其餘部分的時刻。

有誰請解釋如何改變以產生上述的輸出功能?

+0

需要刪除標籤嗎? – 2014-09-26 11:38:36

+0

@RakeshSharma - 刪除任何HTML標籤和縮進。謝謝 – aphextwix 2014-09-26 11:41:33

回答

0

您遇到的問題是因爲preg_split()。這會導致字符串在正則表達式匹配時「爆炸」,這意味着在第一部分只返回,而「修剪」其餘部分。

像這樣的東西應該足夠

<?php 

$s = " <p> 
    In spite of how delightfully smooth the full-grain leather may be, 
    it still provides quite a bit of durability to the upper of this 
    Crush by Durango women&#39;s Western boot. Everything about this 
    pull-on boot is stunning; you have the floral embroidery, the pop 
    of metallic and a stylish x toe. To make these 12&rdquo; floral 
    boots ease to get on, Durango added two very sturdy pull straps 
    next to the boot&rsquo;s opening.</p> 
<ul> 
    <li> 
     2 1/4&quot; heel</li> 
    <li> 
     Composition rubber outsole with vintage finish</li> 
    <li> 
     Cushion Flex insole</li> 
</ul>"; 

echo html_entity_decode(preg_replace("/<.+>/", "", trim($s))); 

https://eval.in/198897