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}.

    点赞 打赏 评论

相关推荐 更多相似问题