【问题标题】:Get some text from long text field in mysql dbase从 mysql dbase 中的长文本字段中获取一些文本
【发布时间】:2013-10-14 09:58:03
【问题描述】:

我有一个保存到 dbase 的文本字段,其中包含很长的电子邮件。如果预览部分说像只查看 250 个单词并放入...或链接以查看剩余部分,我想回应部分。

请帮我写代码

我用普通的

echo $row['email'];

【问题讨论】:

  • 到目前为止你做了什么?发布一些代码,你会得到一些帮助。
  • 为什么要允许这么长的电子邮件?电子邮件地址的最大长度为 254 个字符!看看这个:stackoverflow.com/questions/386294/…
  • 我指的不是电子邮件地址,而是电子邮件。只要美国的历史。

标签: php forms textfield


【解决方案1】:
$email= $row['email'];

if (strlen($email) > 250) {

    // cut the email string
    $emailCut= substr($email, 0, 250);

    //ensure that it ends with a whole word
    $email= substr($emailCut, 0, strrpos($emailCut, ' ')).'... <a href="#">Read More</a>'; 
}
echo $email;

我想这就是你的意思?

【讨论】:

    【解决方案2】:

    我不知道您的完整代码是什么。但是,这个问题可以有多种解决方案。一种可能的解决方案是:

    <?php
     //test.php
     //just this part only,
     $step = isset($_REQUEST['step'])?(int)$_REQUEST['step']:1;
     if($step==1):
     echo substr($row['email'],0,250);
     echo '<a href="test.php?step=2" target="_self">view more</a>';
     elseif($step==2):
     echo substr($row['email'],250,strlen($row['email']));//if you want to display the rest
     //if you want to display the whole text simply echo $row['email']
     endif;
    ?>
    

    我已经测试过了,这对你来说很好。

    【讨论】:

      【解决方案3】:

      您可以使用以下函数来自动换行(utf8-safe!)您的文本并创建链接。 wrap() 将您的文本拆分为数组字符串。这比指定单词的数量更有意义,因为单词可以是很短的“hi”或很长的“hippopotomonstrosesquipedaliophobia”。不要忘记转义输出。

      示例:echo wrappedlink(htmlspecialchars($row['email']), 20);

      function wrappedlink($str, $len) {
          return '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."\n".
              '<a href="javascript:toggleDisplay(\''.($id=substr(md5(rand().$str),0,8)).'\');">'.wrap($str, $len)[0].'</a> <div id="'.$id.'" style="display:none;">'.$str.'</div>';
      }
      
      function wrap($string, $width) {
          if (($len=mb_strlen($string, 'UTF-8')) <= $width) return array(
              $string
          );
          $return=array();
          $last_space=FALSE;
          $i=0;
      
          do {
              if (mb_substr($string, $i, 1, 'UTF-8') == ' ') $last_space=$i;
      
              if ($i > $width) {
                  $last_space=($last_space == 0)?$width:$last_space;
                  $return[]=trim(mb_substr($string, 0, $last_space, 'UTF-8'));
                  $string=mb_substr($string, $last_space, $len, 'UTF-8');
                  $len=mb_strlen($string, 'UTF-8');
                  $i=0;
              }
              $i++;
          } while ($i < $len);
      
          $return[]=trim($string);
          return $return;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-17
        • 2018-10-25
        • 1970-01-01
        • 2021-10-06
        • 2017-12-01
        • 2012-07-21
        • 1970-01-01
        相关资源
        最近更新 更多