PHP Code How To Display Only A Certain Amount Of Characters Limit Letters


PHP Code How To Display Only A Certain Amount Of Characters Limit Letters
  Forums Index
      » PHP Forums
        » » PHP Code How To Display Only A Certain Amount of Characters Limit Letters



PHP Code How To Display Only A Certain Amount Of Characters Limit Letters
Post Description:
Post Tags: php, code, how, to, display, only, a, certain, amount, of, characters, limit, letters, free php support, php scripts, php questions, php answers, programming, codes, scripts
This Post Has Been Viewed 690 Times Since Tue Feb 06, 2007 6:14 pm Posted By hostman with 1 replies
Next Post »» Learn How To Count How Many Characters A String Has In PHP Tutorial PHP
PHP Code How To Display Only A Certain Amount Of Characters Limit Letters
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


     Code:
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:





     Code:
<?

$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:
     Code:
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:



     Code:
<?
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>&nbsp;</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>

Leave Your Comments
* Your Name

  * Your Email Address (Will Not Be Published)

* Your Comments


Add Picture To Comments
Yes No             upload
Receive Replies on my Comments
(An email will be sent to you when someone replies to your comments)

     




Comments and replies About PHP Code How To Display Only A Certain Amount Of Characters Limit Letters






:: 1 :: #274 - Reply By eca On Thu Feb 08, 2007 4:04 pm
this is a good code of php only show so many words in a string. i tried it myself and it works, thants for writing this simple to use tutorial guide.