- Forums
- PHP
- How To Display Certain Amount Of Letters Numbers From Data Get String PHP
limit the amount of characters displayed in a string using php code. here i will show you how to limit the letters or number from a database in a php string. also i give you example code you can try it for youself [995], Last Updated: Sat May 18, 2024
edwino85
Thu Dec 14, 2006
0 Comments
1216 Visits
the PHP substr() function is useful when creating long title links.
For example, i want to create a link with the following text:
This is a very long text link I am creating to show this helpful function in php because i think that this will help others make their websites better and look professional at the same timeok, as you can see from the above link, its way to long. its probably has like 300 characters, but lets say i want to keep this type of link to a maximum of 50 charaters, so i will ONLY display the first 50 characters of the long link to make it short. so the new link should look like this:
This is a very long text link I am creating to ...so how do you do this? PHP offer alot of usefule functions, the substr() will return only a specified number of characters from a string. so you can learn how this function works, i've created a php file you can upload to your site to see it in action:
NOTE: for this function to work, you will need to have PHP, if you don't have PHP on your webstie, we recommend our friends at www.webune.com for PHP hosting. check them out.
Copy and paste the following into your text editor like notepad:
CODE:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>How To Limit Number of Characters Tutorial Guide by Wallpaperama.com
</title>
<style type="text/css">
<!--
.style1 {
color: #FF0000;
font-weight: bold;
}
.style2 {color: #0000FF}
-->
</style>
</head>
<body>
<h1 align="center">How To Limit Number of Characters Tutorial Guide by <a href="http://www.wallpaperama.com">Wallpaperama</a> </h1> <hr />
<p align="center">PHP Hosting at <a href="http://www.webune.com">Webune.com</a> </p>
<?
$link='This is a very long text link I am creating to show this helpful function in php because i think that this will help others make their websites better and look professional at the same time';
?>
<p><strong>This is the value of <span class="style2">$link</span>: </strong></p>
<p><a href="http://www.wallpaperama.com"><?php echo $link; ?></a></p>
<p> </p>
<p><strong>Now this is the link with the <span class="style2">substr()</span> function:</strong></p>
<p><a href="http://www.wallpaperama.com"><?php echo substr($link, 0, 37); ?> ...</a></p>
<p align="left">This is the code i used to display characters from 0 to 35 only: <span class="style1"><?php</span> <span class="style2">echo substr</span>(<span class="style1">$link, 0, 37</span>);<span class="style1"> ?></span></p>
<p align="left"><a href="http://www.wallpaperama.com/forums"><< Go back to Turorial </a></p>
<p align="center">PHP Hosting at <a href="http://www.webune.com">Webune.com</a> </p>
</body>
</html>
now save this file as
function-substr.php and upload to your site, open it with your browser and you will see how it works