Search for characters in a php string

Source: Internet
Author: User

This article describes how to use strstr and strpos functions to query whether the specified string contains any expected characters. For more information, see.

The strstr () function is used to obtain the substring from the first occurrence of a specified string in another string to the end of the string. If the execution is successful, returns the remaining string (matched characters exist). If no matching character is found, false is returned.
Syntax:
String strstr (string haystack, string needle)
Haystack: A required parameter that specifies the string from which to search.
Parameter needle: A required parameter that specifies the search object. If this parameter is a numeric value, it searches for characters that match the ASCII value of this numeric value.
Instance:

The Code is as follows: Copy code
<? Php
Echo strstr ("tomorrow's programming Dictionary", "edited"); // output the query string
Echo "<br>"; // press Enter.
Echo strstr ("www.111c.net", "111"); // output the query string (from the first m value)
Echo "<br>"; // press Enter.
Echo strstr ("0431-84972266", "8"); // output the query string
?>


Definition and usage
The strpos () function returns the position where the string first appears in another string.

If this string is not found, false is returned.

Syntax
Strpos (string, find, start) parameter description
String is required. Specifies the string to be searched.
Find is required. Specifies the characters to be searched.
Start is optional. Specifies the location where the search starts.
Tips and comments
Note: This function is case sensitive. Use the stripos () function to perform case-insensitive searches.
Example

The Code is as follows: Copy code

<? Php
Echo strpos ("Hello world! "," Wo ");
?> Output:

6

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.