Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何确定网页上呈现的字符串的长度?(不使用strlen())_Php_Html - Fatal编程技术网

Php 如何确定网页上呈现的字符串的长度?(不使用strlen())

Php 如何确定网页上呈现的字符串的长度?(不使用strlen()),php,html,Php,Html,如何根据字符数在tr中显示数据 我的代码是: <?php $query = "select text from mytable where id=".$_POST["id"]; $result = mysql_query($query); $row=mysql_fetch_row($result); ?> <table> <tr class="text-center border"> <td><?php echo $r

如何根据字符数在tr中显示数据

我的代码是:

<?php
$query = "select text from mytable where id=".$_POST["id"];

$result = mysql_query($query);

$row=mysql_fetch_row($result);    
?>

<table>
<tr class="text-center border">
     <td><?php echo $row[0]; ?></td>
</tr>
<tr class="text-center border">
     <td></td>
</tr>
<tr class="text-center border">
     <td></td>
</tr>
</table>


默认情况下,如果文本超过了我想在另一行中显示的值,则表格单元格将调整大小以适应所有数据。你不需要自己做这件事

如果您不想使用strlen,可以使用strlu split,例如:

<table>

<?php
$s = "This is a string that I have to display in pieces of the same size in a table.";
$arr = str_split( $s,10 ); // CONVERT STRING IN ARRAY OF PIECES OF 10 CHARS.

for ( $i = 0; $i < count( $arr ); $i++ )
  echo "<tr>" .
       "  <td>" .
            $arr[ $i ] .  // DISPLAY ONE PIECE PER ROW.
       "  </td>" .
       "</tr>";
?>

</table>

您将获得以下信息:

例如,我们将假设一个数组是一个数据库,要测试它,请将其复制粘贴到PHP文件中并在浏览器中打开:

<table border="1">

<?php
$messages = Array( "Hello, dear, I would love to go to the movies tonight, give me a call!",
                   "Sorry, darling, I have an important meeting and I don't want to get fired.",
                   "Don't you love me? Is your job more important than me? You know what? Good bye forever.",
                 );

foreach ( $messages as $msg ) // WALK EACH MESSAGE.
{ $arr = str_split( $msg,10 ); // CONVERT MESSAGE IN ARRAY OF PIECES OF 10 CHARS.
  for ( $i = 0; $i < count( $arr ); $i++ ) // WALK EACH PIECE.
    echo "<tr>" .
         "  <td>" . $arr[ $i ] . "</td>" . // DISPLAY PIECE.
         "</tr>";
}
?>

</table>


如果文本超过了什么?使用strlen()有什么问题?你很容易受到@MarcB的攻击,这就像SQL注入的完美例子:)是的……但这只是一个例子。请改进你的措辞。如果你只想支持更新的浏览器,可以考虑使用CSS文本省略号。至于你的简单任务,
if(strlen($row[0])>$maxChars){echo'long…}或者{echo'short…}
就足够了,我已经试过了,但是我使用文本,如果更改不再起作用。你想要的确切输出是什么,你得到的确切输出是什么?@Ivan,我编辑了答案并添加了另一个与数据库一起工作的代码(我使用数组而不是数据库,您将用mysqli_查询语句替换数组)。