1

i have a paragraph in the database its like

$str ="this is a paragraph i show shortly and when i click on the view more it will show completely for that i am using the ajax and retrieve it "

i show this like

this is a paragraph i show shortly

php for show the first some word is

function chop_string($str, $x)// i called the function  
{
    $string = strip_tags(stripslashes($string)); 
    return substr($string, 0, strpos(wordwrap($string, $x), "\n"));
}

and when user click on the view more it will display rest of it but the problem it that how to skip this this is a paragraph i show shortly and show the rest of it

i want to show the paragraph after the $x on click on view more

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • @Corbin i have tried to do this by chop the first some word i have shown before but its not working fine – NullPoiиteя Jul 09 '12 at 04:27
  • @Corbin any link or just hint will appricated – NullPoiиteя Jul 09 '12 at 04:27
  • 1
    one approach: SUBSTRING_INDEX http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_substring-index –  Jul 09 '12 at 04:28
  • This can be implemented purely client-side (or with a little PHP to help). I retagged with the relevant languages. – Chris Trahey Jul 09 '12 at 04:29
  • @ctrahey i can do this at client side but i think it will consume to much space ,and slow on loading since the paragraph may be >500 words and some images – NullPoiиteя Jul 09 '12 at 04:31
  • If it's a click that loads the rest of the paragraph, then sending less than the full paragraph with the initial response means you need ajax and an additional php script to give you the paragraph. – Chris Trahey Jul 09 '12 at 04:32
  • @ctrahey i want to do this by php – NullPoiиteя Jul 09 '12 at 04:32
  • @ctrahey ya i have done rest of all now i just need to skip the world/line i have already shown – NullPoiиteя Jul 09 '12 at 04:33
  • And how is that delivered to the client? With the initial request? In a subsequent ajax request? Another full page load? – Chris Trahey Jul 09 '12 at 04:36
  • I'm asking because your question specifically mentions responding to a click. PHP simply does not do that. We need to be more articulate about where these events occur and how the data flows *as the user interacts with the app* – Chris Trahey Jul 09 '12 at 04:38
  • i make a ajax connection on the click – NullPoiиteя Jul 09 '12 at 04:40
  • Mayank, I'm not sure of why you would want to do this on the server-side. Could you perhaps explain your reasoning for such? I feel ctrahey makes a fair point that this should be accomplished client side to avoid extra load on the server. – cereallarceny Jul 09 '12 at 05:00
  • @cereallarceny due to the speed because the paragraph may contain the image and it i load the 10 paragraph with >500 word and 10 images i think the loading speed of the page will slow so i am doing this server side – NullPoiиteя Jul 09 '12 at 05:02
  • 1
    I'm not sure how you plan to truncate out images. However, maybe that's the entire reason for your question, in which case I'll admit to whatever your application calls for. Generally though I've found it's almost always better to avoid server calls whenever possible. If you've found what you feel to be an exception to this rule, then I'll trust your judgement. :) – cereallarceny Jul 09 '12 at 05:06

4 Answers4

5

For truncating a string by character of word amount:

  1. This SO question may be of help.
  2. As would this one.
  3. Here's some code that does it specifically by word count.

As far as showing more text on the click of a link goes, what I would suggest is loading the string from the database one time and format it's output. If your string is:

This is whole string pulled from my database.

Then the following code would be formatted like such:

HTML

<p class="truncate">This is the whole string <a class="showMore">Show more...</a><span> pulled from my database.</span></p>

CSS

p.truncate span { display: none; }

That way you can use Javascript (preferably through a library like jQuery which I've chosen for my code below) to hide or show more of your solution without having to make a second database request using AJAX. The following Javascript would do what you're requesting:

$("a.showMore").on("click", function() {
    $(this).parent().find("span").contents().unwrap();
    $(this).remove();
});

Here's a fiddle to play with!

Community
  • 1
  • 1
cereallarceny
  • 4,913
  • 4
  • 39
  • 74
  • You're right. Sorry, I've edited my answer to better reflect the question asked. – cereallarceny Jul 09 '12 at 04:53
  • This is almost exactly the answer I had composed ;-) ... but check out the conversation on the question... asker does not actually want the full text sent with the initial response, there is an ajax call on-click. – Chris Trahey Jul 09 '12 at 04:57
  • Hahaha, glad to know my answer wasn't far from you got. I'm glad to know I didn't have some unorthodox thinking in what I wrote. :) I'm not sure though that this is an issue for the server... nor can I think of a reason why it ever would be. – cereallarceny Jul 09 '12 at 05:01
  • accept and +1 for your great suggestion and also ill do this by jquery – NullPoiиteя Jul 09 '12 at 05:42
2

I have made an example here: shaquin.tk/experiments/showmore.html.

You can view the source to see all of the code behind it. The PHP code is displayed on the page.

If you don't want to display the start string when Show more is clicked, replace the JavaScript function showMore with this:

function showMore() {
    if(state == 0) {
        state = 1;
        document.getElementById('start').style.display = 'none';
        document.getElementById('end').style.display = 'block';
        document.getElementById('showmore').innerHTML = 'Show less';
        document.getElementById('text-content').className = 'expanded';
        document.getElementById('start').className = 'expanded';
    } else {
        state = 0;
        document.getElementById('start').style.display = 'block';
        document.getElementById('end').style.display = 'none';
        document.getElementById('showmore').innerHTML = 'Show more';
        document.getElementById('text-content').className = '';
        document.getElementById('start').className = '';
    }
}

Hope this helps.

uınbɐɥs
  • 7,236
  • 5
  • 26
  • 42
1

Use this function :

function trim_text($string, $word_count)
{
   $trimmed = "";
   $string = preg_replace("/\040+/"," ", trim($string));
   $stringc = explode(" ",$string);
   //echo sizeof($stringc);
   //echo "&nbsp;words <br /><br />";
   if($word_count >= sizeof($stringc))
   {
       // nothing to do, our string is smaller than the limit.
     return $string;
   }
   elseif($word_count < sizeof($stringc))
   {
       // trim the string to the word count
       for($i=0;$i<$word_count;$i++)
       {
           $trimmed .= $stringc[$i]." ";
       }

       if(substr($trimmed, strlen(trim($trimmed))-1, 1) == '.')
         return trim($trimmed).'..';
       else
         return trim($trimmed).'...';
   }
}
Sathishkumar
  • 3,394
  • 4
  • 20
  • 23
0
$wordsBefore = 3;
$numOfWords = 7;
implode(' ', array_slice(explode(' ', $sentence), $wordsBefore, $wordsBefore+$numOfWords));

This will return the first 7 words of a sentence if you save it to a variable named sentence.

Max Hudson
  • 9,961
  • 14
  • 57
  • 107