lukelee Posted December 16, 2008 Share Posted December 16, 2008 form example, in the first page, there is a article, it takes contents from database, if a article is shorter than 1000, it displays the full content on the first page, if its longer than 1000, it will display a short part of article, and displays a 'read more' on the end of the article. how to make this? I think we cant do this by paging function, can we? Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/ Share on other sites More sharing options...
trq Posted December 16, 2008 Share Posted December 16, 2008 Take a look at substr. Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-716360 Share on other sites More sharing options...
br3nn4n Posted December 16, 2008 Share Posted December 16, 2008 There's a topic I started that has code to cut off text at a certain point, in your case 1000 characters Link to index.php?art=(and the article number) then just add in a conditional, to check if art is set in the address bar. If it is then it will show the whole article Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-716424 Share on other sites More sharing options...
lukelee Posted December 16, 2008 Author Share Posted December 16, 2008 There's a topic I started that has code to cut off text at a certain point, in your case 1000 characters Link to index.php?art=(and the article number) then just add in a conditional, to check if art is set in the address bar. If it is then it will show the whole article what is the topic address? thanks Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-716475 Share on other sites More sharing options...
lukelee Posted December 16, 2008 Author Share Posted December 16, 2008 Take a look at substr. I have read about the article, but I couldnt find this method could solve my issue, lets say if my article is "abcdefghijklmn......", and i want to only display first 5 characters, which are "abcde", what should I write? $test="abcdefghijklmn......"; echo substr($test, ?, ?); Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-716536 Share on other sites More sharing options...
gevans Posted December 16, 2008 Share Posted December 16, 2008 From php.net.... string substr ( string $string , int $start [, int $length ] ) Returns the portion of string specified by the start and length parameters. So using your code you want to start from position 0 and display 5 characters finishing at 4. String character positions are like automatic array keys, starting at zero $test="abcdefghijklmn......"; echo substr($test, 0, 4); Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-716537 Share on other sites More sharing options...
lukelee Posted December 16, 2008 Author Share Posted December 16, 2008 From php.net.... string substr ( string $string , int $start [, int $length ] ) Returns the portion of string specified by the start and length parameters. So using your code you want to start from position 0 and display 5 characters finishing at 4. String character positions are like automatic array keys, starting at zero $test="abcdefghijklmn......"; echo substr($test, 0, 4); thanks, it works. now i have another question, I want to display "read more" if the article is longer than ... lets say 1000. does anyone know what function do i use? Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-716634 Share on other sites More sharing options...
trq Posted December 16, 2008 Share Posted December 16, 2008 strlen will get you the length of the string. Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-716682 Share on other sites More sharing options...
9three Posted December 16, 2008 Share Posted December 16, 2008 if strlen($test) > 1000 Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-716704 Share on other sites More sharing options...
lukelee Posted December 18, 2008 Author Share Posted December 18, 2008 thanks, guys Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-718538 Share on other sites More sharing options...
lukelee Posted December 18, 2008 Author Share Posted December 18, 2008 but i got one more question, does 'enter' doesnt include in the length of the string,isnt it? e.g. if i enter acbdefg the length is 7 what about: abc defg is it still 7? because I dont know what content people would enter, if i set echo substr($test, 0, 4); and people enter "abcdefg", it will be something like: abcd read more but when people enter a b c d e f g the read more will be after abcd, which is in the very end. the website position will be very different. any idea how to solve this? Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-718675 Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 The character in between is the \n (new line character) and I believe it is counted as part of the substr, but the best way to find out is test it yourself and see. So go ahead, test it. I promise it won't bite! Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-718684 Share on other sites More sharing options...
lukelee Posted December 18, 2008 Author Share Posted December 18, 2008 The character in between is the \n (new line character) and I believe it is counted as part of the substr, but the best way to find out is test it yourself and see. So go ahead, test it. I promise it won't bite! Its wield, I just tested, if i set substr 5, and type abcdefg, it shows abcde. if i type a b c d e f g it displays: a b but in my setting, one line can contain at least 30 strings Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-718694 Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 So this is how the string is being interupted: "a\nb\nc\n" Since it stops at 5, notice the \n before the c would be at position 5 (note \n is considered 1 character). So imo it works as expected. If you want to evaluate it without the linebreaks, I am not sure the best way to do it, since you probably want to keep the format. But yea, just so you know what is going on. Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-718696 Share on other sites More sharing options...
lukelee Posted December 18, 2008 Author Share Posted December 18, 2008 thanks, i understand it. but there are many website which has 'read more' in the end of the text, click it, it links to another page with a completed article. how does people do that? Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-718728 Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 Ok, so let's say after 100 characters you want to cut off the text and display a link and ... <?php $text = "This is some random text that I am writing here to demonstrate a point. It just has to be longer than 100 characters for this to actually work properly.\nHowever there is a line break now and hopefully that is also counted as part of the string length to help this point be proven. Next time I will just copy and paste a nice description from some unknown site so that I do not have to type this much information here. Cause yea it just really sucks!"; if (strlen($text) > 100) { $shortText = substr($text, 0, 100); $shortText .= '... (<a href="article.php?id=' . $id . '">Read More</a>)'; }else { $shortText = $text; } echo $shortText; ?> Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-718739 Share on other sites More sharing options...
lukelee Posted December 18, 2008 Author Share Posted December 18, 2008 Ok, so let's say after 100 characters you want to cut off the text and display a link and ... <?php $text = "This is some random text that I am writing here to demonstrate a point. It just has to be longer than 100 characters for this to actually work properly.\nHowever there is a line break now and hopefully that is also counted as part of the string length to help this point be proven. Next time I will just copy and paste a nice description from some unknown site so that I do not have to type this much information here. Cause yea it just really sucks!"; if (strlen($text) > 100) { $shortText = substr($text, 0, 100); $shortText .= '... (<a href="article.php?id=' . $id . '">Read More</a>)'; }else { $shortText = $text; } echo $shortText; ?> thanks for ur help. what if i type an article with lots "change new line" ? e.g. a b c d e f g... then it might display something like: a b c d e f g ...(read more) I set the text div 200X200, so, the shortText will break the box, sorry about my poor english, wish you understand what i mean Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-718834 Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 <?php $text = "This is some random text that I am writing here to demonstrate a point. It just has to be longer than 100 characters for this to actually work properly.\nHowever there is a line break now and hopefully that is also counted as part of the string length\n\n to help this point be proven. Next time I will just copy and paste a nice description from some unknown site so that I do not have to type this much information here. \n\nCause yea it just really sucks!"; if (strlen($text) > 100) { $shortText = substr($text, 0, 100); $shortText .= '... (<a href="article.php?id=' . $id . '">Read More</a>)'; }else { $shortText = $text; } echo $shortText; ?> Just create test cases with different scenarios such as the modified above. Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-718887 Share on other sites More sharing options...
lukelee Posted December 18, 2008 Author Share Posted December 18, 2008 <?php $text = "This is some random text that I am writing here to demonstrate a point. It just has to be longer than 100 characters for this to actually work properly.\nHowever there is a line break now and hopefully that is also counted as part of the string length\n\n to help this point be proven. Next time I will just copy and paste a nice description from some unknown site so that I do not have to type this much information here. \n\nCause yea it just really sucks!"; if (strlen($text) > 100) { $shortText = substr($text, 0, 100); $shortText .= '... (<a href="article.php?id=' . $id . '">Read More</a>)'; }else { $shortText = $text; } echo $shortText; ?> Just create test cases with different scenarios such as the modified above. what do u mean by test cases? people could enter anything from cms, so i never know whats gonna be $text. Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-718895 Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 <?php $text = "This is some random text that I am writing here to demonstrate a point. It just has to be longer than 100 characters for this to actually work properly.\nHowever there is a line break now and hopefully that is also counted as part of the string length\n\n to help this point be proven. Next time I will just copy and paste a nice description from some unknown site so that I do not have to type this much information here. \n\nCause yea it just really sucks!"; if (strlen($text) > 100) { $shortText = substr($text, 0, 100); $shortText .= '<br />... (<a href="article.php?id=' . $id . '">Read More</a>)'; }else { $shortText = $text; } echo $shortText; ?> Just create test cases with different scenarios such as the modified above. what do u mean by test cases? people could enter anything from cms, so i never know whats gonna be $text. You should have some idea of what the user is entering, you always test what users should enter in and validate input. A huge part of programming is testing and create test cases so you can make sure some weird code would not break your code. As far as them creating a new line, for each letter, what does it matter if the read more is on its own line? You could always put a break before displaying the the "read more" as in above. Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-718899 Share on other sites More sharing options...
popularanky Posted March 10, 2012 Share Posted March 10, 2012 I just came across this code and want to use it on a project. But I am having little challenge. Which is: I am a variable "pro_desc" (which holds my article) in a grid table "problem".How do I use the variable to replace your ("This is some random text that I am writing here to demonstrate a point. It just has to be longer than 100 characters for this to actually work properly.\nHowever there is a line break now and hopefully that is also counted as part of the string length\n\n to help this point be proven. Next time I will just copy and paste a nice description from some unknown site so that I do not have to type this much information here. \n\nCause yea it just really sucks!") Thanks Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-1325963 Share on other sites More sharing options...
redsmurph Posted March 10, 2012 Share Posted March 10, 2012 Articles normally contain very few line breaks (\n) as each paragraph is technically one line of text. Hence you can handle line breaks the same way as other characters. One thing you might want to do though is to secure that the last word of the page is not cut in half. Especially if you use UTF-8 this can otherwise create garbage characters. In that case I recommend setting mbstring.func_overload = 7. If you want to show full words, then you have to search for the last separator (" " would suffice) in reverse and in the string segment you are going to show, and shorten the string accordingly and then compensate for that "loss" on the next page. Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-1325970 Share on other sites More sharing options...
Pikachu2000 Posted March 10, 2012 Share Posted March 10, 2012 Let's not dredge up threads that are over three years old, mkay? Start your own thread, with your own question, and if necessary add a link to the old thread. Link to comment https://forums.phpfreaks.com/topic/137133-how-to-make-a-read-more-function/#findComment-1325981 Share on other sites More sharing options...
Recommended Posts