Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7851761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:15:35+00:00 2026-06-02T19:15:35+00:00

Possible Duplicate: Object could not be converted to string? i need to create random

  • 0

Possible Duplicate:
Object could not be converted to string?

i need to create random title to my images and i got a error

Object of class MPRandomText could not be converted to string in
C:\Program Files (x86)\EasyPHP-5.3.9\www\dir.php on line 42

and how can i include the random text to my html image title ?

    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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:15:35+00:00Added an answer on June 2, 2026 at 7:15 pm

    $title is an instance of MPRandomText. You concatenate $title in the output but MPRandomText does not implement __toString(), so it can not be converted into a string.

    <?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()
    }
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: difference between string object and string literal Let's say I have two
Possible Duplicate: What are the reasons why Map.get(Object key) is not (fully) generic According
Possible Duplicate: C++: What is the size of an object of an empty class?
Possible Duplicate: Pick random property from a Javascript object suppose I have a javascript
Possible Duplicate: PHP ToString() equivalent I get this error: Catchable fatal error: Object of
Possible Duplicate: PHP class instantiation. To use or not to use the parenthesis? Omission
Possible Duplicate: Printing a string to a temporary stream object in C++ std::ostringstream printing
Possible Duplicate: Java Thread Garbage collected or not Consider the following class: class Foo
Possible Duplicate: Comparing object properties in c# Let's say I have a POCO: public
Possible Duplicate: Hibernate: different object with the same identifier value was already associated with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.