控制页面中输出字符串的长度

一 代码

1、index.php
<?php 
	include_once("conn/conn.php"); //调用连接数据库的文件 
	include_once("function.php");
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>控制页面中输出字符串的长度</title>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
a:link {
	text-decoration: none;
	color: #666666;
}
a:visited {
	text-decoration: none;
	color: #FF0000;
}
a:hover {
	text-decoration: none;
	color: #666666;
}
a:active {
	text-decoration: none;
	color: #666666;
}
-->
</style></head>
<body>
<table width="1147" height="813" border="0" align="center" cellpadding="0" cellspacing="0" >
  <tr>
    <td width="115" height="438">&nbsp;</td>
    <td width="378">&nbsp;</td>
    <td width="654">&nbsp;</td>
  </tr>
  <tr>
    <td height="121">&nbsp;</td>
    <td align="center" valign="top">
	<table width="365" height="22" border="0" align="center" cellpadding="0" cellspacing="0">
      <?php
				 $sql=mysql_query("select * from tb_new_dynamic order by id desc limit 0,6",$id);
				while($myrow=mysql_fetch_array($sql)){
				 ?>
      <tr>
        <td width="20" height="22"><div align="center"><img src="images/01.JPG"/></div></td>
        <td width="258" height="22"><a href="new_dynamic.php?id=<?php echo $myrow["id"];?>" title="<?php echo $myrow["dynamic_title"];?>">
          <?php 						
						echo msubstr($myrow["dynamic_title"],0,24);
						
						 if(strlen($myrow["dynamic_title"])>24){
			                echo " ... ";
			             }
							
					  ?>
        </a> </td>
        <td width="87"><?php  echo "<font color=red>[".substr(str_replace("-","/",$myrow[createtime]),0,10)."]</font>";?></td>
      </tr>
      <tr>
        <td colspan="3"></td>
      </tr>
      <?php			  }
			
				 ?>
    </table></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>
 
2、function.php
<?php
function msubstr($str,$start,$len) {					//定义自定义函数的名称,控制文本输出字符的个数
    $strlen=$start+$len;								//获取文本的长度
    for($i=0;$i<$strlen;$i++) { 						//循环输出文本中的字符
        if(ord(substr($str,$i,1))>0xa0) { 				//截取文本中的字符
            $tmpstr.=substr($str,$i,2);					//截取文本中的字符
            $i++; 
        }else 
            $tmpstr.=substr($str,$i,1); 
    } 
    return $tmpstr;								 
}
?>
 
3、new_dynamic.php
<?php 
	include_once("conn/conn.php"); //调用连接数据库的文件 
	include_once("function.php");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>最新动态详细内容</title>
<style type="text/css">
<!--
.STYLE1 {
	color: #666666;
	font-size: 13px;
}
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
-->
</style>
</head>
<body>
<table width="1003" height="620" border="0" align="center" cellpadding="0" cellspacing="0" background="images/02.JPG">
  <tr>
    <td width="402" height="186">&nbsp;</td>
    <td width="458">&nbsp;</td>
    <td width="143">&nbsp;</td>
  </tr>
  <tr>
    <td height="341">&nbsp;</td>
	  <?php
				 $sql=mysql_query("select * from tb_new_dynamic where id='".$_GET[id]."'",$id);
				while($myrow=mysql_fetch_array($sql)){
				 ?>
    <td align="left" valign="top" class="STYLE1"><span class="STYLE1"><?php echo $myrow[dynamic_title];?>
      <br>
      <br>
      <?php echo $myrow[dynamic_content];?></span></td>
	<?php }?>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>
 
4、conn.php
<?php 
	$id=mysql_connect('localhost','root','root');	//连接数据库服务器
    mysql_select_db('db_database06',$id);			//连接db_database06数据库
	mysql_query("set names gb2312");				//指定数据库中字符的编码格式
?>
 
二 运行效果

 

猜你喜欢

转载自cakin24.iteye.com/blog/2351281