- Forums
- PHP
- PHP Code How To Display Only A Certain Amount Of Characters Limit Letters
This Page Contains information about PHP Code How To Display Only A Certain Amount Of Characters Limit Letters By wallpaperama in category PHP with 3 Replies. [1006], Last Updated: Sat May 18, 2024
wallpaperama
Tue Feb 06, 2007
3 Comments
5751 Visits
substr($string, 0,100)
thanks to the fold at www.webune.com for their help on this question i had with my php. i host with them and they asked me to write a tutorial just incase there are oher people who want to know how they can disaly only whatever amount of character they want from a string.
so lets say i have a long string, but when i want to show it on my website it doesnt' fit because the string has too many characters or letters or words and i only want to show only a limited about of words. so lets say my string is called $string and this is the value of $string
I am writing this tutorial to show you how you can cut down on the words with php. if you need php web hosting, our friends at webune.com have the right solution for you. dedicated hosting is the best way to go if you have a large website. as you can see, this string is pretty long.
if you count the number of character in the paragraph above you can see i have 283 character, so lets say i only want to display 100 this is the code i would use:
<?
$string ="I am writing this tutorial to show you how you can cut down on the words with php. if you need php web hosting, our friends at webune.com have the right solution for you. dedicated hosting is the best way to go if you have a large website. as you can see, this string is pretty long.";
echo substr($string, 0,100);
?>
this will be the output:
I am writing this tutorial to show you how you can cut down on the words with php. if you need php w
i wrote up an example script you can use so that you can see it in action. just remember, you must have PHP on your web site. if you don't have PHP, you can visit our friends at www.webune.com and signup with the with PHP hosting.
copy and paste the following code into notepad and save it as remove-chars.php then upload to your site and open it with you browser to see how it works:
<?
echo "<h1>I will limit 100 character in a string</h1><hr>";
$string ="I am writing this tutorial to show you how you can cut down on the words with php. if you need php web hosting, our friends at webune.com have the right solution for you. dedicated hosting is the best way to go if you have a large website. as you can see, this string is pretty long.";
echo "this is <b>\$string</b> before substr() function: $string <BR><BR>";
echo "this is how many character i have in <b>\$string:</b> <br><b>".strlen($string)."</b><BR><BR>";
$string = substr($string, 0,100);
echo "this is <b>\$string</b> after substr() function: <br><b>$string</b>";
?>
<p> </p>
<p>Dont forget to tell your friends about where they can find free amazing wallpapers, only at Wallpaperama!</p>
<p align="center">Script by <a href="http://www.wallpaperama.com">wallpaperama.com</a> </p>
<p align="center">PHP Hosting at <a href="http://www.webune.com">Webune.com </a></p>
hope this helps