2013-08-06 24 views
2

這是我的函數:爆炸並留下定界符

$words = explode('. ',$desc); 
     $out = ''; 
     foreach ($words as $key => $value) { 
      $out .= $value; 
      $rand = rand(0, 4); 
      if ($rand == 2) { 
       $out .= "\n"; 
      } else { 
       $out .= ' '; 
      } 
     } 

總之它插入隨機點後,新的生產線,但在這種情況下,點被刪除。

我該怎麼辦explode('. ',$desc),並在他們的位置留下點?

回答

1

當您連接時,只需將它們放回即可。

$words = explode('. ',$desc); 
$out = ''; 
foreach ($words as $key => $value) { 
    $out .= $value.'.'; 
    $rand = mt_rand(0, 4); 
    if ($rand == 2) { 
     $out .= "\n"; 
    } else { 
     $out .= ' '; 
    } 
} 

您還應該使用mt_rand(),它是一個更好的版本的rand()除非你喜歡不是真的隨機結果。

+0

媽呀,你是對的,只是 - 爲什麼我沒有tgouht吧:) – user2606353

+0

很大,也感謝意見 – user2606353

0

正面看後面沒有正則表達式主題。

$words = preg_split('/(?<=\.)(?!\s*$)/', $desc); 

分裂在任何非字符與它後面的dot

0

試試這個代碼..

$words = explode('. ',$desc); 

      $out = ''; 
      foreach ($words as $key => $value) { 

       $out .= $value; 
       $rand = rand(0, 4); 
       if ($rand == 2) { 
        $out .= "<br>"; 
       } else { 
        $out .= ' '; 
       } 
      }