【问题标题】:Php split echo by counting characters echoed, add variable content and then keep echoingphp通过计算回显的字符来拆分回显,添加变量内容,然后继续回显
【发布时间】:2015-10-13 00:17:31
【问题描述】:

需要我的 php 代码来计算回显文本中的字符数。当这个计数达到 64 时,我需要它来回显“$something”并从它停止的地方继续回显。

此外,最好的情况是这段代码不应该裁剪完整的单词。

例如

-- 这个:

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"; 

但这只是呼应了我文字的第一行。

【问题讨论】:

  • 您需要调查输出缓冲。在 Google 上搜索 ob_start。
  • 我试过..... echo mb_strimwidth($row['resumen'], 0, 84, "$something"); echo mb_strimwidth($row['resume'], 64, 64, "$something"); echo mb_strimwidth($row['resume'], 128, 64, "$something");

标签: php count character echo


【解决方案1】:

终于找到了这个解决方案:

$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";

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 2014-01-28
    • 2011-04-21
    相关资源
    最近更新 更多