Jump to content

how to make a read more function?


lukelee

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

<?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
Share on other sites

<?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
Share on other sites

<?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
Share on other sites

  • 3 years later...

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
Share on other sites

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
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • 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.