Extracting Substrings

You want to extract part of a string, starting at a particular place in the string. For example, you want the first eight characters of a username entered into a form.

 Extracting a substring with substr( )

<?php
$substring = substr($string,$start,$length);
$username = substr($_GET['username'],0,8);
?>
 
If $start and $length are positive, substr( ) returns 
$length characters in the string, starting at $start.
 
If $start is bigger than the length of the string, substr( ) returns false..
 


Related Posts:
  • php interview questions PHP Interview Questions and Answers Click  this  link   … Read More
  • php exec The exec() function is one of several functions you can use to pass commands tothe shell. The exec() function requires a string representing the path to the commandyou want to run, and optionally accepts an array variable th… Read More
  • Storing Complex Data Types You can use sessions to store complex data types such as objects and arrays simply by treating them as standard variables, as this code shows: $myarr["0"] = "Sunday"; $myarr["1"] = "Monday"; $myarr["2"] = "Tue… Read More
  • What is LAMP? LAMP means combination of Linux, Apache, MySQL and PHP. … Read More
  • mod_rewrite? Rewrites the requested URL on-the-fly based on configuration directives and rules. You are using system paths. Apache mod_rewrite only works with URLs,   RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} … Read More
  • Pagination in PHP and MySQL <?php function pagination($per_page = 10, $page = 1, $url = '', $total){ $adjacents = "2"; $page = ($page == 0 ? 1 : $page); $start = ($page - 1) * $per_page; $prev = $page - 1; $next = $page + 1; $lastpage = ceil… Read More
  • Check if image file ?? allow_url_fopen is activated in your PHP config     $filename = "http://".$_SERVER['SERVER_NAME']." /media/img/".$row['CatNaam'].".jpg"; echo" <img src=\"".$filename."\" alt=\"".$row['CatNaam']."\">"; … Read More
  • Encoding php file str_rot13() base64_decode()   Try ionCube PHP Encoder,  link - http://www.ioncube.com/sa_encoder.php        There are only two worthwhile players in the encoding market, Zend Guard ($600) and i… Read More
  • Use $_POST to get input values It will execute the whole file as PHP. The first time you open it,  $_POST['submit']  won't be set because the form has not been sent.    <?php if (isset($_POST['submit'])) { $example = $_POST['… Read More
  • PHP Write and Read from File $fp = @fopen ("text1.txt", "r"); $fh = @fopen("text2.txt", 'a+'); if ($fp) { //for each line in file while(!feof($fp)) { //push lines into array $thisline = fgets($fp); $thisline1 = trim($thisline); … Read More
  • thumbnail creation PHP script   To create a thumbnail, first check file entenson, and then read in the file using the imagecreatefromjpeg() or imagecreatefrompng() or imagecreatefromgif() function and can calculate the new thumbnail size. imag… Read More
  • php file_get_contents This function is the preferred way to read the contents of a file into a string. The function itself does nothing but puts the source of the web page you supply it into a string available for us… Read More
  • What are the different tables present in mysql? Total 5 types of tables we can create 1. MyISAM 2. Heap 3. Merge 4. InnoDB 5. ISAM 6. BDB MyISAM is the default storage engine … Read More
  • Cannot modify header information Check that <?php is at the very start of the functions.php file (before any whitespace) and remove ?> at the end of that file. header or setcookie or anything else that sends HTTP headers has  to be done before&n… Read More
  • How get the value of current session id? Session_id() returns the session id for the current session. … Read More