如何在PHP中显示MySQL列的部分文本

时间:2017-08-23 04:34:01

标签: php mysql

我有这张表plans

id| plan_name| description
1 | basic    | Good plan for startups <br>
               Excellent quality <br>
               Lorem ipsum Lorem

我想echo只使用PHP中的查询description列中的前两行。

查询

  $getuserplaninfo=mysqli_query($db, "SELECT * FROM plans WHERE membershipID='$userplanID'");
  $userplaninforesults=mysqli_fetch_assoc($getuserplaninfo);
  $userplaninfo=$userplaninforesults['description'];
  echo $userplaninfo;

结果应为

Good plan for startups
Excellent quality

2 个答案:

答案 0 :(得分:3)

使用explode

尝试此操作
$userplaninfo = "Good plan for startups <br>
Excellent quality <br>
Lorem ipsum Lorem";
$userplaininfo_array = explode("<br>",$userplaninfo);

$userplaininfo = $userplaininfo_array[0];
if(isset($userplaininfo_array[1]))
{
    $userplaininfo .= "<br>".$userplaininfo_array[1];
}
echo $userplaininfo;

DEMO

答案 1 :(得分:0)

您可以使用PHP substr函数获取子字符串。

$userplaninfo = substr($userplaninforesults['description'], 45);