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

时间:2015-06-24 21:51:12

标签: 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 $row[0]; ?></td>
</tr>
<tr class="text-center border">
     <td></td>
</tr>
<tr class="text-center border">
     <td></td>
</tr>
</table>

如果文字超出我想要在另一行tr。

中显示

2 个答案:

答案 0 :(得分:0)

默认情况下,表格单元格会调整大小以适合所有数据。你自己不需要这样做。

答案 1 :(得分:0)

如果您不想使用strlen,可以使用str_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>

你会得到这个:

enter image description here

我们假装的数组示例是一个数据库,测试它将其复制粘贴到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>