PHP/String/substr — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 07:07, 26 мая 2010
Содержание
- 1 Accessing Substrings: string substr ( string string, int start [, int length] )
- 2 A negative substr() length parameter
- 3 A positive substr() length parameter
- 4 Extracting a substring with substr()
- 5 Extracting Part of a String with substr()
- 6 Extracting the end of a string with substr()
- 7 If you pass substr() a negative number, it counts from the end of the string.
- 8 Reading Fixed-Width Delimited Data
- 9 Setting up and working with both strings and substrings
- 10 Single-character substitutions
- 11 Specify a negative number as parameter three for the length
- 12 string substr ( string str, int start_pos [, int length] ) reads part of a string and takes a minimum of two parameters
- 13 substr-2.php
- 14 substr-3.php
- 15 substr() function returns the part of the string between the start and start+length parameters.
- 16 substr.php
- 17 Truncating a string with substr()
- 18 Using negative lengths allows you to say "copy everything but the last three characters,"
- 19 Using substr() with length past the end of the string
- 20 Using substr() with negative length
- 21 Using substr() with negative start
- 22 Using substr() with positive $start and $length
- 23 Using substr() with positive start and no length
Accessing Substrings: string substr ( string string, int start [, int length] )
<?php
$theclientstext = "this is a test. this is another test";
if (strlen ($theclientstext) >= 30){
echo substr ($theclientstext,0,29);
} else {
echo $theclientstext;
}
?>
A negative substr() length parameter
<?
$car = "1234567890";
$yr = substr($car, 2, -5);
print $yr;
?>
A positive substr() length parameter
<?
$car = "1234567890";
$yr = substr($car, 0, 4);
print $yr;
?>
Extracting a substring with substr()
<?php
$substring = substr($string,$start,$length);
$username = substr($_GET["username"],0,8);
?>
Extracting Part of a String with substr()
<?php
$test = "this is a test";
print substr($test,6);
print substr($test,6,2);
?>
Extracting the end of a string with substr()
<?
print "Card: XX";
print substr("this is a test. this is a test",-4,4);
?>
If you pass substr() a negative number, it counts from the end of the string.
<?php
$test = "this is a test.";
if ( $test = substr( $test, -3 ) == "est") {
print "est";
} else {
print "Welcome to our shop!";
}
?>
Reading Fixed-Width Delimited Data
<?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.<br />";
}
} else {
echo "File does not exist.";
}
?>
Setting up and working with both strings and substrings
<?php
$mystring = "Hello World!";
echo $mystring . "<br />";
echo substr ($mystring,0,5);
?>
Single-character substitutions
<?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);
?>
Specify a negative number as parameter three for the length
<?
$string = "Goodbye, Perl!";
$a = substr($string, 5, 5);
$b = substr($string, 5, -1);
$c = substr($string, 0, -7);
?>
string substr ( string str, int start_pos [, int length] ) reads part of a string and takes a minimum of two parameters
<?
$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);
?>
substr-2.php
<?php
$car = "2009 Ford";
echo substr($car, 0, 4);
?>
substr-3.php
<?php
$car = "2009 Ford";
echo substr($car, 2, -5);
?>
substr() function returns the part of the string between the start and start+length parameters.
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;
?>
substr.php
<?php
$car = "2009 Ford";
echo substr($car, 5);
?>
Truncating a string with substr()
<?
print substr("this is a test", 0, 3);
print "...";
?>
Using negative lengths allows you to say "copy everything but the last three characters,"
<?
$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);
?>
Using substr() with length past the end of the string
<?
print substr("This is a test. This is another test.",20,5);
?>
Using substr() with negative length
<?
print substr("This is a test. This is another test.",15,-2);
print substr("This is a test. This is another test.",-4,-1);
?>
Using substr() with negative start
<?
print substr("This is a test. This is another test.",-6);
print substr("This is a test. This is another test.",-17,5);
?>
Using substr() with positive $start and $length
<?
print substr("This is a test. This is another test.",6,5);
?>
Using substr() with positive start and no length
<?
print substr("This is a test. This is another test.",17);
?>