"Php-06 string and its functions"

Source: Internet
Author: User

The string is a sequence of characters, such as "Hello world!".

One, PHP string functions

1. The strlen () function returns the length of the string, measured in characters.

For example: Returns the length of the string "Hello world!":

<?php Echo strlen ("Hello world!"); ?>

Results: 12

  2. the Strpos () function is used to retrieve the specified character or text within a string.

For example: Find out where world is appearing in Hello World:

<?php Echo Strpos ("Hello world!", "World"); ?>

Results: 6

Note: The position of the first character in the string is 0 instead of 1.

3. str_split ( string, Length ) takes a length length, the default is 1, to progressively split the string, returning an array.

<?php Print_r (Str_split ("Shanghai", 3));? >

Result: Array ([0] = Sha [1] = Ngh [2] = = AI)

4. Explode ( separator,string,limit ):

    Separator: Required, specifies in which way the string is divided;

String: The strings to be split

Limit: Do not write by default to string length, otherwise the following:

        • Greater than 0-returns an array containing a maximum of limit elements
        • Less than 0-returns an array containing all elements except the last-limit element
        • 0-Returns an array containing an element
<?php $str = ' one,two,three,four ';  0 Limit Print_r (Explode (', ', $str, 0));  Positive limit Print_r (explode (', ', $str, 2)); Negative limit Print_r (explode (', ', $str,-1));? >

Result: Array ([0] = one,two,three,four)

Array ([0] = one [1] = Two,three,four)

Array ([0] = one [1] = [2] = three)

  5. substr ( string, start, length): Intercepts the string at start position, length does not write to the end of the string.

<?php    Echo substr ("Hello World", 10). " <br> ";    echo substr ("Hello World", 1). " <br> ";    echo substr ("Hello World",-1). " <br> ";    echo substr ("Hello World",-10). " <br> ";? >

Results: D
Ello World

D
Ello World

6. Mentioned in 5 above. (point), and. =, is a connector, it can also be omitted in a string.

Second, the common output statement

Echo ()
Multiple values can be output at one time, and multiple values are separated by commas. Echo is a language structure (language construct), not a real function and therefore cannot be used as part of an expression.

Print ()
Print () output string. print () is not actually a function (it's a language structure) so it can't be called by a mutable function, so you don't have to use parentheses to enclose its argument list.

Print_r ()
You can simply print the string and the number, and the array is displayed with the enclosed key and the list of merits, starting with an array. However, the result of Print_r () output Boolean and null is meaningless because it is all printed "\ n". Therefore, using the Var_dump () function is more suitable for debugging.

Var_dump ()
Determine the type and length of a variable, and output the value of the variable if the variable has a value for the variable and the return data type. This function displays structure information about one or more expressions, including the type and value of the expression. The array recursively expands the value and displays its structure by indentation.

"Php-06 string and its functions"

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.