Php 无法在中将类MPRandomText的对象转换为字符串

Php 无法在中将类MPRandomText的对象转换为字符串,php,string,class,object,Php,String,Class,Object,可能重复: 我需要为我的图片创建随机标题,但我遇到了一个错误 无法在中将类MPRandomText的对象转换为字符串 第42行的C:\Program Files(x86)\EasyPHP-5.3.9\www\dir.php 如何将随机文本包含到html图像标题中 class MPRandomText { var $filepath; var $sepstring; var $fixchar; var $textfix; var $cont

可能重复:

我需要为我的图片创建随机标题,但我遇到了一个错误

无法在中将类MPRandomText的对象转换为字符串 第42行的C:\Program Files(x86)\EasyPHP-5.3.9\www\dir.php

如何将随机文本包含到html图像标题中

    class MPRandomText
    {
    var $filepath;
    var $sepstring;
    var $fixchar;
    var $textfix;
    var $contents;
    var $random;
    var $errors;
    // initiate object and start functions
    function MPRandomText($filepath, $sepstring, $textfix)
        {
        $this->filepath = $filepath;
        $this->textfix = $textfix;
        $this->sepstring = $sepstring;
        $this->errors = "";
        $this->contents = "";
        $this->FileToString();
        }
    // read file contents into string variable
    function FileToString()
        {
        if (!$this->filepath || !file_exists($this->filepath))
            $this->errors = "Could not find text file at ".$this->filepath;
            else {
            @$filePointer = fopen($this->filepath, "r");
            if (!$filePointer) $this->errors = "Text file could not be opened.";
            if ($this->errors == "")
                {
                $this->contents = fread($filePointer, filesize($this->filepath));
                fclose($filePointer);
                if (!$this->textfix) $this->HTMLContent();
                $this->MakeArray();
                }
            }
        }
    // if importing an HTML page, drop everything outside of (and including) the <body> tags
    function HTMLContent()
        {
        $test = stristr($this->contents, "<body");
        if ($test !== false)
            {
            $test = stristr($test, ">");
            if ($test !== false)
                {
                $test = str_replace("</BODY>", "</body>", $test);
                $test = explode("</body>", substr($test, 1));
                if (count($test) > 1) $this->contents = $test[0];
                }
            }
        }
    // convert the file text into a list using separation character
    function MakeArray()
        {
        $array = explode($this->sepstring, $this->contents);
        if (count($array) > 0)
            {
            $this->contents = $array;
            $this->CleanTextList();
            } else $this->errors = "Text file contents are empty or could not be read.";
        }
    // clean up the list of empty values and extra white space
    function CleanTextList()
        {
        $result = array();
        if (is_array($this->contents))
            {
            for ($n=0; $n<count($this->contents); $n++)
                {
                $string = trim($this->contents[$n]);
                $test = trim($string."test");
                if (!empty($string) AND $test != "test") $result[] = $string;
                }
            if (count($result) > 0)
                {
                $this->contents = $result;
                $this->RandomString();
                }
            }
        }
    // get random text string from list
    function RandomString()
        {
        reset($this->contents);
        srand((double) microtime() * 1000000);
        shuffle($this->contents);
        $this->random = $this->contents[0];
        }
    // send finished results to be printed into HTML page
    function GetResults()
        {
        if ($this->errors != "") return $this->errors;
            else
            {
            if ($this->textfix == true) $this->random = htmlentities($this->random);
            return $this->random;
            }
        }
    }

//-------------------------------------------------------------------------------------------------
//  FUNCTION AND VARIABLE TO CREATE RANDOM TEXT INSTANCE
//-------------------------------------------------------------------------------------------------

// create variable to store instance references
$MPRandomTextHandles = array();

// function to create new handle and import random text
function MPPrintRandomText($MPTextFile = "random.txt", $MPSepString = "*divider*", $MPTextToHTML = false)
    {
    global $MPRandomTextHandles;
    for ($n=0; $n<250; $n++)
        {
        if (!isset($MPRandomTextHandles[$n]))
            {
            $MPRandomTextHandles[$n] = new MPRandomText($MPTextFile, $MPSepString, $MPTextToHTML);
            break;
            }
        }
    print($MPRandomTextHandles[$n]->GetResults());
    return $MPRandomTextHandles[$n];
    }


    /* Начало конфигурации */
$thumb_directory = 'img/thumbs';
$orig_directory = 'img/imag';
/* Конец конфигурации */
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$title1='גילוף פסלים בעץ';
$i=0;


/* Открываем папку с миниатюрами и пролистываем каждую из них */
$dir_handle = @opendir($thumb_directory) or die("There is an error with your 
image directory!");
$i=1;

while ($file = readdir($dir_handle))
{
/* Пропускаем системные файлы: */
if($file=='.' || $file == '..') continue;
$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));

/* Используем название файла (без расширения) в качестве названия изображения: */
// $title = implode('.',$file_parts);
// $title = htmlspecialchars($title);
include_once("GetRandomText.php");
$MPTextFile = "random.txt";
$MPSepString = "*divider*";
$MPTextToHTML = false;
$title = MPPrintRandomText($MPTextFile, $MPSepString, $MPTextToHTML);

/* Если расширение разрешено: */
if(in_array($ext,$allowed_types))
{

/* Выдаем каждое фото: */
echo '<li>
        <a href="'.$orig_directory.'/'.$file.'" rel="lightbox[pasel]">
            <img title="'.$title.'" src="'.$thumb_directory.'/'.$file.'" width="72" height="72" alt="גילוף פסלים בעץ" />
        </a>
    </li>';
}
}
/* Закрываем папку */
closedir($dir_handle);
class文本
{
var$filepath;
var$sepstring;
var$fixchar;
var$textfix;
var$内容;
var$random;
var$错误;
//启动对象和启动函数
函数MPRandomText($filepath、$sepstring、$textfix)
{
$this->filepath=$filepath;
$this->textfix=$textfix;
$this->sepstring=$sepstring;
$this->errors=“”;
$this->contents=“”;
$this->FileToString();
}
//将文件内容读入字符串变量
函数FileToString()
{
如果(!$this->filepath | |!file_存在($this->filepath))
$this->errors=“在“$this->filepath”中找不到文本文件;
否则{
@$filePointer=fopen($this->filepath,“r”);
如果(!$filePointer)$this->errors=“无法打开文本文件。”;
如果($this->errors==“”)
{
$this->contents=fread($filePointer,filesize($this->filepath));
fclose($filePointer);
如果(!$this->textfix)$this->HTMLContent();
$this->MakeArray();
}
}
}
//如果导入HTML页面,请删除标记之外(包括)的所有内容
函数HTMLContent()
{
$test=stristr($this->contents,“”);
如果($test!==false)
{
$test=str_replace(“,”,$test);
$test=explode(“”,substr($test,1));
如果(计数($test)>1)$this->contents=$test[0];
}
}
}
//使用分隔字符将文件文本转换为列表
函数MakeArray()
{
$array=explode($this->sepstring,$this->contents);
如果(计数($array)>0)
{
$this->contents=$array;
$this->CleanTextList();
}else$this->errors=“文本文件内容为空或无法读取。”;
}
//清理空值和额外空白的列表
函数CleanTextList()
{
$result=array();
if(是数组($this->contents))
{
对于($n=0;$n内容);$n++)
{
$string=trim($this->contents[$n]);
$test=修剪($string.“test”);
如果(!empty($string)和$test!=“test”)$result[]=$string;
}
如果(计数($result)>0)
{
$this->contents=$result;
$this->RandomString();
}
}
}
//从列表中获取随机文本字符串
函数RandomString()
{
重置($this->contents);
srand((双)微时间()*1000000);
洗牌($this->contents);
$this->random=$this->contents[0];
}
//发送完成的结果以打印到HTML页面
函数GetResults()
{
如果($this->errors!=“”)返回$this->errors;
其他的
{
如果($this->textfix==true)$this->random=htmlentities($this->random);
返回$this->random;
}
}
}
//-------------------------------------------------------------------------------------------------
//用于创建随机文本实例的函数和变量
//-------------------------------------------------------------------------------------------------
//创建变量以存储实例引用
$MPRandomTextHandles=array();
//函数创建新句柄并导入随机文本
函数MPPrintRandomText($MPTextFile=“random.txt”,$MPSepString=“*divider*”,$MPTextToHTML=false)
{
全球$MPT文本句柄;
对于($n=0;$nGetResults());
返回$MPRandomTextHandles[$n];
}
/* Начало конфигурации */
$thumb_目录='img/thumbs';
$orig_目录='img/imag';
/* Конец конфигурации */
$allowed_types=数组('jpg'、'jpeg'、'gif'、'png');
$file_parts=array();
$ext='';
$title='';
$title1=';
$i=0;
/* Открываем папку с миниатюрами и пролистываем каждую из них */
$dir\u handle=@opendir($thumb\u directory)或die(“您的
图片目录!);
$i=1;
而($file=readdir($dir\u handle))
{
/* Пропускаем системные файлы: */
如果($file=='。| |$file=='..)继续;
$file_parts=分解('.',$file);
$ext=strtolower(数组_pop($file_parts));
/* Используем название файла (без расширения) в качестве названия изображения: */
//$title=内爆('.',$file\u parts);
//$title=htmlspecialchars($title);
包括_once(“GetRandomText.php”);
$MPTextFile=“random.txt”;
$MPSepString=“*分隔符*”;
$MPTextToHTML=false;
$title=MPPrintRandomText($MPTextFile、$MPSepString、$MPTextToHTML);
/* Если расширение разрешено: */
if(在数组中($ext,$allowed_types))
{
/* Выдаем каждое фото: */
回声'
  • '; } } /* Закрываем папку */ closedir($dir_handle);
    $title是MPRandomText的一个实例。您在输出中连接$title,但MPRandomText未实现_toString(),因此无法将其转换为字符串

    <?php 
    $title = MPPrintRandomText($MPTextFile, $MPSepString, $MPTextToHTML);
    // MPPrintRandomText returns an instance of MPRandomText
    
    /* Если расширение разрешено: */
    if(in_array($ext,$allowed_types))
    {
    
    /* Выдаем каждое фото: */
    echo '<li>
        <a href="'.$orig_directory.'/'.$file.'" rel="lightbox[pasel]">
            <img title="'.$title.'" src="'.$thumb_directory.'/'.$file.'" width="72"     height="72" alt="גילוף פסלים בעץ" />
        </a>
    </li>';
    // concatenating $title here will fail because MPRandomText does not implement __toString()
    }
    }
    ?>
    
    
    
    如何将其转换为字符串?要么在MPRandomText中实现一个uu-toString()方法,要么使用任何您想要的字符串,例如$title->RandomString()。我不知道如何使其工作:(