PHP Common String Function Summary _php tutorial

Source: Internet
Author: User
Tags chop rtrim sprintf strtok
1. Formatted output

Chop is the alias of RTrim ();

LTrim ()

Trim ()

NL2BR () converts \ n to


print,echo,printf (), sprintf ():

Echo () is not a function, print () is a function, has a return value, boolen,false,true;

printf () Formatted output

--function, the text formatted after the output, directly call the system call for IO, he is non-buffered. Such as:
$name = "Hunte";
$age = 25;
printf ("My name is%s, age%d", $name, $age);

sprintf () Formats the string and assigns it to a variable, but does not output, similar to C.

echo nl2br ("foo isn ' t\n bar");
echo "foo isn ' t\n bar";
?>

-Similar to printf, but not print, but return formatted text, the rest is the same as printf. Such as:
Char sql[256];
sprintf (SQL, "select * FROM table where no = '%s '", Bankno);
Its function simply assigns the statement inside the "" to the variable SQL.

Strtolower

Strtoupper

Ucwords

Ucfirst

2. Connection and segmentation of strings

(1) array explode (string input, string separator, int limit)

Use one string to split another string

Example 1
$pizza = "Piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = Explode ("", $pizza);
echo $pieces [0]; Piece1
echo $pieces [1]; Piece2

Example 2
$data = "Foo:*:1023:1000::/home/foo:/bin/sh";
List ($user, $pass, $uid, $gid, $gecos, $home, $shell) = Explode (":", $data);
Echo $user; Foo
Echo $pass; // *

?>


Example 2. Example of the limit parameter

$str = ' One|two|three|four ';

A positive limit
Print_r (Explode (' | ', $STR, 2));

Limit of negative numbers
Print_r (Explode (' | ', $STR,-1));
?>



String Strtok (string input, string separator)

$string = "This Is\tan example\nstring";
/* Use tab and newline as tokenizing characters as well */
$tok = Strtok ($string, "\n\t");

Holding a space, \n,\t as a token split string

while ($tok!== false) {
echo "Word= $tok
";
$tok = Strtok ("\n\t");
}
?>

Results:

Word=this
Word=is
Word=an
Word=example
Word=string

(2.) Interception of strings

$test = "Your customer service is excellent";
Echo substr ($test, 1);////////our customer service is excellent
echo "
";
Echo substr ($test,-9);//////length from the end is 9excellent
echo "
";
Echo substr ($test, 0,4);////starting from 0 position length is 4Your
echo "
";
Echo substr ($test, 5,-13);/starting from fourth to 13th character of customer service
echo "
";

$test = "Your customer service is excellent";
Echo substr ($test, 1);
echo "
";
Echo substr ($test,-11);
echo "
";
Echo substr ($test, 0,6);
echo "
";
Echo substr ($test, 5,-13);
echo "
";

Our customer service is excellent
S excellent
Your C
Customer Service

(3) Link to join () string

3. Searching for strings


(1) string strstr (String haystack, string needle) aliases: Strchr,stristr and STRSTR are similar in that they are case insensitive

STRRCHR () Instead, looks for the last occurrence of the string

The first occurrence of the string
$email = ' user@example.com ';
$domain = Strstr ($email, ' @ ');
Echo $domain; Prints @example. com
?>
$email = ' user@example.com ';
$domain =strstr ($email, ' e ');
$domain 2 =strrchr ($email, ' e ');//The last occurrence of a string
Echo $domain;
echo "
";
echo $domain 2;

Er@example.com
E.com

(2) Find location

int Strpos (string str,string needle,[int offset]) did not find the return is False

Returns the location of the needle found in Str starting with offset

$eg: $t-' Hello World ';

Echo Strpos ($t, ' O ', 5);

7 starting from O, find the position of the variable o, the result is 7

int Strrpos ()

5. Replace

Str_replace ("%body%", "Blank", "





6. Small capitalization issues


Strpos
Finds the position of the first occurrence of a string in a string

Strrpos
Finds a character in a string, followed by the first occurrence of a position after the initial time.

Strpos (Stripos not case)
Strrpos (Strripos not case)






Strstr
STRISTR (no case)

Str_replace
Str_ireplace (no case)

http://www.bkjia.com/PHPjc/318672.html www.bkjia.com true http://www.bkjia.com/PHPjc/318672.html techarticle 1. Formatted output chop is an alias for RTrim (), LTrim () trim () nl2br () convert \ n to BR print,echo,printf (), sprintf (): Echo () is not a function, print () is a function, There are return values, Boolen, ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.