Jump to content

Recommended Posts

Hi Everyone,

 

I have a string that has been truncated with having the last word intact. This string contains Read More phrase in the end. So, if I click on this Read More phrase, I redirect it to the another page. I want to keep it on the same page and want to expand the full text (that was previously truncated). Do I need to use <div> for that along with AJAX or only <div> is enough??

 

If anyone has a code snippet related to my problem then it can give me a right way to move ahead...

 

Any help regarding this will be highly appreciated.

 

Thanks

Anuj

Link to comment
https://forums.phpfreaks.com/topic/154530-truncated-text/
Share on other sites

Thanks mjdamato for your quick reply...

But in the post (link), you provided here, is showing the text that completely hides and shows on clicking the link.

Will the foolowing example given by you provides the exact result what I am looking for (without splitting by HTML tags within the text):

 

$index = 1;

$maxLength = 50; //max characters to show for the preview

while ($record = mysql_fetch_assoc($result))

{

    $text = $record['text'];

 

    //Determin if preview is needed

    $preview = (strlen($text)>$maxLength);

 

    if ($preview)

    {

        echo "<a href=\"#\" onclick=\"showHide({$index});\" id=\"display_{$index}\">Show</a><br>\n";

    }

 

    echo "<div id=\"text_{$index}\">\n";

    if ($preview)

    {

        //Preview needed, break out the text on the space before $maxLength

 

        //Determine the break point

        $tempString = substr($text, 0, $maxLength+1);

        $breakPoint = strrpos($tempString, ' ');

        //Display the text parts

        echo "<span>" . substr($text, 0, $breakPoint) . "</span>";

        echo "<span id=\"ellipse_{$index}\">...</span>";

        echo "<span id=\"full_{$index}\" style=\"display:none;\">" . substr($text, $breakPoint) . "</span>";

    }

    else

    {

        //No preview needed, display entire text string

        echo $text;

    }

    echo "</div><br><br>\n";

    $index++;

}

Link to comment
https://forums.phpfreaks.com/topic/154530-truncated-text/#findComment-812572
Share on other sites

Either you are not understanding the code provided int he otehr post or I am not undestanding you.

 

Here is what I understood in your first post:

You stated that you are displaying text which has been truncated and that upon clicking the "Read More" link the user is redirected to another page to read the entire text. instead you wnat the entire text to be displayed on the same page without a redirect.

 

That is what I provided in the code in the other post I directed you to. The code snipped you posted above was the code to be run on the PHP side to determine what truncated and full text to be used. There is no splitting by HTML tags. You apparently already have code to truncate how you wish, so you would just need to use the code above as a guide to modify your current process. The full text needs to be split into the truncated and non-truncated text and put into DIV or SPAN containers, which the JS code will then use to display/hide the truncated text.

Link to comment
https://forums.phpfreaks.com/topic/154530-truncated-text/#findComment-812623
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.