dswqw66280 2014-01-19 21:53
浏览 24
已采纳

如何在这个类中添加PHP wordwrap?

I need to do PHP wordwrap not CSS of {file:filename}. Any help would be sincerely appreciated.

Here's an example from the PHP Manual -

<?php
$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "
", true);
echo "$newtext
";
?>

Heres class Fileitem that contains $filename, I'm assuming the wordwrap should go somewhere in here.

<?php
class FileItem extends Item
{
/**
 * @param string $fn The filename
 * @return string Everything after the list dot in the filename, not including the dot
 */
public static function ext($fn)
{
    $fn = Item::get_basename($fn);
    return (strpos($fn, '.') ? strtolower(substr(strrchr($fn, '.'), 1)) : '');
}

/**
 * @return string Returns the extension of the filename
 * @see FileItem::ext()
 */
public function file_ext()
{
    return self::ext($this -> filename);
}

/**
 * @param string $parent_dir
 * @param string $filename
 */
public function __construct($parent_dir, $filename)
{
    parent::__construct($parent_dir, $filename);
    if (!@is_file($this -> parent_dir . $filename))
    {   
        throw new ExceptionDisplay('File <em>'
        . Url::html_output($this -> parent_dir . $filename)
        . '</em> does not exist.');
    }
    global $config, $words, $downloads;
    $this -> filename = $filename;
    $this -> size = new Size(filesize($this -> parent_dir . $filename));
    if (ICON_PATH)
    {
        $file_icon = new Icon($filename);
        $this -> icon = $file_icon -> __toString();
    }
    $this -> downloads = (DOWNLOAD_COUNT && $downloads -> is_set($parent_dir . $filename) ? (int)($downloads -> __get($parent_dir . $filename)) : 0);
    $this -> link = Url::html_output($_SERVER['PHP_SELF']) . '?dir=' . Url::translate_uri(substr($this -> parent_dir, strlen($config -> __get('base_dir'))))
    . '&amp;file=' . Url::translate_uri($filename);
    if (THUMBNAIL_HEIGHT && in_array(self::ext($filename), array('png', 'jpg', 'jpeg', 'gif')))
    {
        $this -> thumb_link = ' <img src="' . Url::html_output($_SERVER['PHP_SELF'])
        . '?thumbnail='. Url::translate_uri($this -> parent_dir . $filename)
        . '" alt="' . $words -> __get('thumbnail of') . ' ' . $filename
        . '" />';
    }
    $size = $this -> size -> __get('bytes');
    if (MD5_SHOW && $size > 0 && $size / 1048576 <= $config -> __get('md5_show'))
    {
        $this -> md5_link = '<span class="autoindex_small">[<a class="autoindex_a" href="'
        . Url::html_output($_SERVER['PHP_SELF']) . '?dir='
        . Url::translate_uri(substr($this -> parent_dir, strlen($config -> __get('base_dir'))))
        . '&amp;md5=' . Url::translate_uri($filename) . '">'
        . $words -> __get('calculate md5sum') . '</a>]</span>';
    }
}

/**
 * @param string $var The key to look for
 * @return mixed The data stored at the key
 */
public function __get($var)
{
    if (isset($this -> $var))
    {
        return $this -> $var;
    }
    throw new ExceptionDisplay('Variable <em>' . Url::html_output($var)
    . '</em> not set in FileItem class.');
}
}
?>

This is where filename outputs in a .tpl file

 <tr class="{file:tr_class}">
  <td class="auto_td">
  <p class="Column1"><a class="auto_a" href="{file:link}">
    {if:icon_path}<img width="16" height="16" alt="[{file:file_ext}]" src="{file:icon}" />{end if:icon_path}
    {file:filename} {file:thumbnail}
   </a>{file:new_icon}{file:md5_link}{file:delete_link}{file:rename_link}{file:edit_description_link}{file:ftp_upload_link}
   </p>
  </td>
  <td class="autoindex_td_right">
    <div class="Column2">
   {file:size}
    </div>
  </td>
  {if:description_file}
  <td class="description">
    <p class="Column3">
  {file:description}
    </p>
  </td>
  {end if:description_file}
  <td class="download">
    <p class="Column4">
    <a href="{file:link}" class="dlButton">Download</a>
    </p>
  </td>
 </tr>
  • 写回答

1条回答 默认 最新

  • duan_2000 2014-01-19 22:03
    关注

    Did you try with <br /> instead of ?

    $newtext = wordwrap($text, 8, "<br />", true);
    

    In your class, add a variable and name it filenameWrap for example.

    Then, in your class constructor, do this:

    $this->filenameWrap = wordwrap($filename, 8, "<br />", true);
    

    And in your template file, call {file:filenameWrap} instead of {file:filename}.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值