2011-10-06 18 views
1

如何在前100個字符後添加<br />?我的意思是打破一次,而不是每100個字符後加<br />php僅在前100個字符後添加<br />

$str="Apple has announced that co-founder, former CEO and chairman of the board Steve Jobs has passed away at the age of 56, so we take a look back at the famous innovator's life.At left, Steve Jobs, chairman of the board of Apple Computer, leans on the new 'Macintosh' personal computer following a shareholder's meeting Jan. 24, 1984, in Cupertino, Calif. The Macintosh was priced at $2,495."; 
//echo substr($str, 0, 100).'<br />'; this will break sentence after 100 characters. 
//echo wordwrap($str, 100, "\n<br />\n");this will add <br /> after each 100 characters. 

而我需要的是:

Apple has announced that co-founder, former CEO and chairman of the board Steve Jobs has passed away  
<br /> <-- only add here. 
at the age of 56, so we take a look back at the famous innovator's life.At left, Steve Jobs, chairman of the board of Apple Computer, leans on the new 'Macintosh' personal computer following a shareholder's meeting Jan. 24, 1984, in Cupertino, Calif. The Macintosh was priced at $2,495. 
+0

會的'$值str'有哪些字符集編碼? UTF-8? –

+0

UTF-8。所有英文單詞。 – cj333

回答

3

您可以使用substr拿到第100個字符,並添加一個<br />當時和之後獲得的所有其它字符。例如:

<?php 
    echo substr($string , 0, 100) 
     , "<br />" 
     , substr($string, 100); 
?> 
+0

嘿!這與我的代碼非常相似。我驚訝地發佈在你面前!哈哈;)沒關係。它獲得GPL許可。希望它有助於@ cj333 – santiagobasulto

+0

是的,我認爲你更好。包含文檔鏈接是很好的。我總是忘記這一點。 – santiagobasulto

+0

關於文檔,[你實際應該看看這個](http://stackoverflow.com/questions/7679607/php-add-br-only-after-first-100-characters/7679871#7679871)。 – hakre

1
$str="Apple has announced that co-founder, former CEO and chairman of the board Steve Jobs has passed away at the age of 56, so we take a look back at the famous innovator's life.At left, Steve Jobs, chairman of the board of Apple Computer, leans on the new 'Macintosh' personal computer following a shareholder's meeting Jan. 24, 1984, in Cupertino, Calif. The Macintosh was priced at $2,495."; 
$first_part = substr ($str ,0,100); 
$second_part = substr ($str ,100); //Goes to end of str 
$new = $first_part."<br />".$second_part; 
6

如果你的意思是寫而不是字符 「後100個字母分手」,然後substr不會做。並且wordwrap確實反覆工作。所以,你需要手動找到100個字母和注入休息有:

$str = preg_replace('/^.{100,}?\b/s', "$0<br>", $str); 
+0

+1爲優雅的解決方案,使我看起來不好大聲笑。 – shuniar

+0

+1。我會建議類似的東西。 – Spudley

+0

這將'
'放在字符串的末尾,一種解決方法是將最大值設置爲100('{0,100} +')或給出一個範圍。 [我選擇範圍](http://stackoverflow.com/questions/7679607/php-add-br-only-after-first-100-characters/7679871#7679871)。 – hakre

0

我會做這樣的事情

<? 
    $str="Apple has announced that co-founder, former CEO and chairman of the board Steve Jobs has passed away at the age of 56, so we take a look back at the famous innovator's life.At left, Steve Jobs, chairman of the board of Apple Computer, leans on the new 'Macintosh' personal computer following a shareholder's meeting Jan. 24, 1984, in Cupertino, Calif. The Macintosh was priced at $2,495."; 

    for ($i = 0; $i < strlen($str); $i += 99) 
    { 
     if ((strlen($str) - $i) < 100) { 
      echo substr($str, $i, strlen($str)); 
     } else { 
      echo substr($str, $i, 100); 
     } 

     echo "<br />"; 
    } 
?> 
5

PHP有一個功能在特定位置插入一個字符串到另一個,這就是所謂的substr_replace

echo substr_replace($str, '<br>', 100, 0); 

如果您需要支持UTF-8(未指定任何編碼),你可以用正則表達式和做到這一點允許不剪的話,太:

echo preg_replace('/^.{80,120}+\b/su', '$0<br>', $str); 
+0

+1以獲得正確的UTF-8解決方案。 –

+0

+1爲一個整潔的解決方案 – vascowhite

+0

+1偉大的答案,偉大的文檔順便說一句! – santiagobasulto

0

這可能工作:

$text = "..."; 
if (isset($text[100])) { 
    $text[100] = $text[100] . "<br />"; 
} 
相關問題