2015-10-13 26 views
1

需要我的PHP代碼來計算被回顯的文本中的字符。當這個數字達到64,我需要它回聲「$東西」,並從它停止的地方不斷迴應。通過計數字符回顯php分割回顯,添加可變內容,然後保持回顯

此外,最好的情況下這種代碼不應該裁剪完整的單詞。

例如

- 此:

echo 'This is a huge string that i mean to crop acording to it\'s character\'s count. For every 64 characters including spaces i need it to echo some other thing in the middle'; 

- 最終會是這樣的:

echo 'This is a huge string that i mean to crop acording to it\'s ' . $something . 'character\'s count. For every 64 characters including spaces ' . $something . 'i need it to echo some other thing in the middle'; 

爲了更好地理解......我需要這個代碼來解決這個事實該SVG文本不能被包裝和對齊。

您會使用mb_strimwidth嗎?怎麼樣?

在此先感謝!

---更新1 - 我已經嘗試過失敗

echo mb_strimwidth($row['resumen'], 0, 84, "$something"); 
echo mb_strimwidth($row['resumen'], 64, 64, "$something"); 
echo mb_strimwidth($row['resumen'], 128, 64, "$something"); 

---更新2 - 部分成功!

$uno = substr($row['resumen'], 0, 64); 
$dos = substr($row['resumen'], 64, 64); 
$tres = substr($row['resumen'], 128, 64); 

$suma = $uno . "</text><text>" . $dos . "</text><text>" . $tres; 
echo "$suma"; 

但這恰恰呼應我的文字的第一行。

+1

你需要調查輸出緩衝。搜索Google搜索ob_start。 –

+0

我試過..... echo mb_strimwidth($ row ['resumen'],0,84,「$ something」); echo mb_strimwidth($ row ['resumen'],64,64,「$ something」); echo mb_strimwidth($ row ['resumen'],128,64,「$ something」); –

回答

0

終於到該解決方案:

$n=0; 

$var="Texto largo mayor a 64 caracteres que complica mi utilizacion de una infografia en svg, ya que este lenguaje no acepta wrappers para el texto. Era muy lindo para ser verdad."; 

$ts= mb_strwidth($var); 

//Ahora defino una variable que cuenta el texto que queda por imprimir. 
$aimprimir=mb_strwidth($var); 

if ($ts>64){ 

    while ($aimprimir>64): 
    //mientras reste por imprimir un texto de largo mayor que 64.... 
    echo mb_strimwidth($var,$n,70,"<br/>"); 
    $aimprimir=$aimprimir-64; 
    $n=$n+65; 
    endwhile; 

    //si lo que resta por imprimir es menor o igual a 64 entonces imprimalo... 
    echo mb_strimwidth($var,$n,$aimprimir,"<br/>"); 

} 

else { 

echo "$var"; 

}