douang4294 2013-04-10 21:53
浏览 20
已采纳

在HTML中抓取变量到Echo的部分

How how would I just grab the first few words of a variable such as $data for example. I'm trying to create an article preview section and that variable is an article. I'm trying to display of course only a preview of it.

    <?php
$sql = "SELECT author, data, postdate, Title, previewimg FROM articlepreview ORDER BY RAND() LIMIT 1";
$query = mysqli_query($db_conx,$sql);
$prev = "";
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    $a = $row["author"];
    $data = $row["data"];
    $postdate = $row["postdate"];
    $title = $row["Title"];
    $previewimg = $row["previewimg"];
    $prev .= '
<div class="wrapPreviewArticle">


    <div style="headerPreview"><h4>'.$title.'</h4></div>
    <div class="wrapPreviewRow">
    <div style="imagePreview"><img src="'.$previewimg.'" height="20%" width="20%" alt="'.$title.'" /></div>
    <div clas="wordPreview">'.$data.'</div>
    </div>
</div>
';
}
?>

Once I've got this I would spit out the variable

<body>
<div><?php echo $prev; ?></div> <!--with only the desired portion.-->
</body>

Any help is greatly appreciated.

  • 写回答

3条回答 默认 最新

  • douxuqiao6394 2013-04-10 21:55
    关注

    You could use SUBSTRING_INDEX on your query:

    "SELECT author, SUBSTRING_INDEX(data, ' ', 5) data, ... FROM articlepreview ..."
    

    This will extract only the first 5 words from the field data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义