“Read More” Link / Add Limit to Text In PHP

CODE

EXPLANATION

Line Five Strip Tags are used to remove any html tag present in the string and returns a pure string without any html tag .

For more Documentation On Strip Tags Click Here

Line Seven is a condition that if the limit exceeds 500  then our functionality will move further . If you want to change the limit make changes in $wordLimit  variable .

Line Ten uses substr which is cuts the whole string from position 0 to $wordlimit which is 500 .

For documentation on Sub String Click Here

Now we have a string starting from position 0 to 500, but we don’t want our words to get cut off in the last . lets say at last position¬† we had a word technofusions.com but on 500th character was “h” so the last word would become “tech” instead of technofusions.com . So for that we have the next line of code .

$string = substr($stringCut, 0, strrpos($stringCut, ' '))

strrpos find the position of last word after space and then again we cut the string using substr.

For documentation on String Pos Click Here

Now in the end we want to concat the anchor tag to our detailed string page .

That’s It !

If you have any questions feel free to comment .

Leave a Reply

Your email address will not be published. Required fields are marked *