PHP Common String Function Summary _php tips

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

Chop is the alias of RTrim ();

LTrim ()

Trim ()

NL2BR () convert \ n to <br>

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 format of the text after the output, directly call the system call for IO, he is not buffered. Such as:
$name = "Hunte";
$age = 25;
printf ("My name was%s, age%d", $name, $age);

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

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

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

Strtolower

Strtoupper

Ucwords

Ucfirst

2. Connection and segmentation of strings

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

Split another string using one string

<?php
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. Limit parameter Example

<?php
$str = ' One|two|three|four ';

Limit of positive numbers
Print_r (Explode (' | ', $STR, 2));

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



String Strtok (string input, string separator)

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

Take a space, \n,\t the string as a token

while ($tok!== false) {
echo "word= $tok <br/>";
$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 "<br>";
Echo substr ($test,-9);//////from the end length is 9excellent
echo "<br>";
Echo substr ($test, 0,4)////starting from 0 position length is 4Your
echo "<br>";
Echo substr ($test, 5,-13);/from start fourth to last 13th character customer service
echo "<br>";

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

Our customer service is excellent
S excellent
Your C
Customer Service

(3) Link of join () string

3. Find the String


(1) string strstr (String haystack, string needle) alias: Strchr,stristr and strstr are similar except that they are case-insensitive

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

The first occurrence of the string
<?php
$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 ');//last Occurrence of string
Echo $domain;
echo "<br>";
echo $domain 2;

Er@example.com
E.com

(2) Find location

int Strpos (string str,string needle,[int offset]) not found to return false

Returns the position of needle from offset starting in Str

$eg: $t-' Hello World ';

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

7 starting at O, find the position of this variable, and the result is 7.

int Strrpos ()

5. Replace

Str_replace ("%body%", "Blank", "<body text= '%body% '")






6. Small capitalization problem


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

Strrpos
Finds a character in a string, following the first occurrence of the position.

Strpos (Stripos no case)
Strrpos (Strripos no case)






Strstr
STRISTR (no case)

Str_replace
Str_ireplace (no case)

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.