簡體   English   中英

如何在此類中添加PHP自動換行?

[英]How can I add PHP wordwrap to this class?

我需要做PHP換行而不是{file:filename}的CSS。 任何幫助將由衷的感謝。

這是PHP手冊中的示例-

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

這里的類Fileitem包含$ filename,我假設自動換行應該放在這里的某個地方。

<?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.');
}
}
?>

這是.tpl文件中文件名輸出的位置

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

您是否嘗試使用<br />而不是\\n

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

在您的類中,添加一個變量並將其命名為filenameWrap

然后,在您的類構造函數中,執行以下操作:

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

然后在模板文件中,調用{file:filenameWrap}而不是{file:filename}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM