确保第n个单词后的换行符

时间:2015-01-11 15:19:43

标签: php regex

我有以下脚本将文本转换为图像,但问题是它在第n个字符后中断并忽略单词,因此最终输出令人困惑。

我能够确保第一行以单词结束,但我希望所有行都相同,以便每行以单词结束,新行以下一个单词开头。这是我的代码

   <?php
  $fileName = 'test.png';
  $img_width = 800;
  $img_height = 90;
  $font = 'Parisish.ttf';    
  $string = 'Tujhse doori ka ehsas satane laga,
    Tere sath gujra har lamha yaad aane laga,
 Jab bhi koshish ki tujhe bhulne ki ae mere hamdum…
     Tu aur jyada dil ke kareeb aane laga…';
   $string = mb_convert_encoding($string, 'UTF-8',"UTF-8");

 $curstringLen = strlen($string);
  $limit = 40;
  $totalLine = ceil($curstringLen / $limit);
  $img_height = $img_height * $totalLine;

  $im = imagecreatetruecolor($img_width, $img_height);
   $white = imagecolorallocate($im, 255, 255, 255);
  $grey = imagecolorallocate($im, 128, 128, 128);
   $black = imagecolorallocate($im, 0, 0, 0);

  imagefilledrectangle($im, 0, 0, $img_width, $img_height, $white);

  for($i = 1; $i <= $totalLine; $i++){


   $y = $i * 70;

    $stringN = substr($string,($limit * ($i-1)),strpos($string, ' ', $limit));



   imagettftext($im, 20, 0, 50, $y, $grey, $font,mb_convert_encoding($stringN, 'UTF-8', 'UTF-8'));
      imagettftext($im, 20, 0, 51, $y, $black, $font,mb_convert_encoding($stringN, 'UTF-8', 'UTF-8'));
  }
   imagepng($im, $fileName); 
 ?>

它有效,但仅为英文字符提供输出,并在不考虑单词

的情况下突破限制线

0 个答案:

没有答案