PHP/String/substr

Материал из Web эксперт
Перейти к: навигация, поиск

Accessing Substrings: string substr ( string string, int start [, int length] )

   <source lang="html4strict">

<?php $theclientstext = "this is a test. this is another test"; if (strlen ($theclientstext) >= 30){

   echo substr ($theclientstext,0,29); 

} else {

   echo $theclientstext; 

} ?>

 </source>
   
  


A negative substr() length parameter

   <source lang="html4strict">

<? $car = "1234567890"; $yr = substr($car, 2, -5); print $yr; ?>

 </source>
   
  


A positive substr() length parameter

   <source lang="html4strict">

<? $car = "1234567890"; $yr = substr($car, 0, 4); print $yr; ?>

 </source>
   
  


Extracting a substring with substr()

   <source lang="html4strict">

<?php $substring = substr($string,$start,$length); $username = substr($_GET["username"],0,8); ?>

 </source>
   
  


Extracting Part of a String with substr()

   <source lang="html4strict">

<?php

   $test = "this is a test";
   print substr($test,6); 
   print substr($test,6,2);

?>

 </source>
   
  


Extracting the end of a string with substr()

   <source lang="html4strict">

<? print "Card: XX"; print substr("this is a test. this is a test",-4,4); ?>

 </source>
   
  


If you pass substr() a negative number, it counts from the end of the string.

   <source lang="html4strict">

<?php $test = "this is a test."; if ( $test = substr( $test, -3 ) == "est") {

 print "est";

} else {

 print "Welcome to our shop!";

} ?>

 </source>
   
  


Reading Fixed-Width Delimited Data

   <source lang="html4strict">

<?php

 $flatfile = "data.txt";
 if (file_exists ($flatfile)){
   $rows = file ($flatfile);
   for ($i = 0; $i < count ($rows); $i++){
     $item = substr ($rows[$i],0,20);
     $amount = substr ($rows[$i],20,40);
     echo "Item: " . rtrim ($item) . " has " . rtrim ($amount) . " unit (s) left.
"; } } else { echo "File does not exist."; }

?>

 </source>
   
  


Setting up and working with both strings and substrings

   <source lang="html4strict">

<?php

   $mystring = "Hello World!"; 
   echo $mystring . "
"; echo substr ($mystring,0,5);

?>

 </source>
   
  


Single-character substitutions

   <source lang="html4strict">

<?php

 $qwerty = "qwertyuiopasdfghjklzxcvbnm" . "QWERTYUIOPASDFGHJKLZXCVBNM";
 $dvorak = "abcdefghijklmnopqrstuvwxyz" . "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 $message = "Start the attack when the sun rises.";
 $encoded = strtr($message, $qwerty, $dvorak);
 $decoded = strtr($encoded, $dvorak, $qwerty);

?>

 </source>
   
  


Specify a negative number as parameter three for the length

   <source lang="html4strict">

<?

   $string = "Goodbye, Perl!";
   $a = substr($string, 5, 5);
   $b = substr($string, 5, -1);
   $c = substr($string, 0, -7);

?>

 </source>
   
  


string substr ( string str, int start_pos [, int length] ) reads part of a string and takes a minimum of two parameters

   <source lang="html4strict">

<?

   $message = "Goodbye, Perl!";
   $a = substr($message, 1);
   $b = substr($message, 0);
   // $b contains the full string because we started at index 0
   $c = substr($message, 5);
   $d = substr($message, 50);
   $e = substr($message, 5, 4);
   $f = substr($message, 10, 1);

?>

 </source>
   
  


substr-2.php

   <source lang="html4strict">

<?php

  $car = "2009 Ford";
  echo substr($car, 0, 4);

?>

 </source>
   
  


substr-3.php

   <source lang="html4strict">

<?php

  $car = "2009 Ford";
  echo substr($car, 2, -5);

?>

 </source>
   
  


substr() function returns the part of the string between the start and start+length parameters.

   <source lang="html4strict">

Its syntax is: string substr (string string, int start [, int length]) If start is positive, returned substring starts at the start"th position of the string. If start is negative, returned substring starts at the string (length - start"th position of the string. If length is positive, the returned substring consists of the characters between start and (start + length). If this distance is greater than the distance between start and the end of string, then only the substring between start and the string"s end will be returned. If length is provided and is negative, the returned substring will end length characters from the end of string. <? $car = "1234567890"; $model = substr($car, 6); print $model; ?>

 </source>
   
  


substr.php

   <source lang="html4strict">

<?php

  $car = "2009 Ford";
  echo substr($car, 5);

?>

 </source>
   
  


Truncating a string with substr()

   <source lang="html4strict">

<? print substr("this is a test", 0, 3); print "..."; ?>

 </source>
   
  


Using negative lengths allows you to say "copy everything but the last three characters,"

   <source lang="html4strict">

<?

   $string = "Goodbye, Perl!"
   $a = substr($string, 5);
   $b = substr($string, 5, 5);
   $c = substr($string, 0, -1);
   $d = substr($string, -5);
   $e = substr($string, -5, 4);
   $e will be set to "Perl"
   $f = substr($string, -5, -4);

?>

 </source>
   
  


Using substr() with length past the end of the string

   <source lang="html4strict">

<? print substr("This is a test. This is another test.",20,5); ?>

 </source>
   
  


Using substr() with negative length

   <source lang="html4strict">

<? print substr("This is a test. This is another test.",15,-2); print substr("This is a test. This is another test.",-4,-1); ?>

 </source>
   
  


Using substr() with negative start

   <source lang="html4strict">

<? print substr("This is a test. This is another test.",-6); print substr("This is a test. This is another test.",-17,5); ?>

 </source>
   
  


Using substr() with positive $start and $length

   <source lang="html4strict">

<? print substr("This is a test. This is another test.",6,5); ?>

 </source>
   
  


Using substr() with positive start and no length

   <source lang="html4strict">

<? print substr("This is a test. This is another test.",17); ?>

 </source>