String function library-search type

Source: Internet
Author: User
Compatible: (PHP3, PHP4, PHP5) strpos -- Findpositionoffirstoccurrenceofastring: position syntax for the first occurrence of a character string: intstrpos (stringhaystack, mixedneedle [, javasffset]) return value: integer function type: data processing content description: English: Retu compatible :( PHP 3, PHP 4, PHP 5)
Strpos -- Find position of first occurrence of a string
Find the position where the character appears for the first time.
Syntax: int strpos (string haystack, mixed needle [, int offset])
Return value: integer
Function type: data processing

Description:
English:
Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos (), this function can take a full string as the needle parameter and the entire string will be used.
If needle is not found, strpos () will return boolean FALSE.

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack.

Chinese:
The position where the return parameter needle appears for the first time in the haystack string, expressed in numbers. Unlike strrpos (), this function can take all strings of the needle parameter and use all strings. If the needle parameter is not found, false is returned.

If the needle parameter is not a string, it is converted to an integer and used according to the character sequence value.
The offset parameter allows you to specify which character to start searching in the haystack. the returned position is still the starting point relative to the haystack.

* It is worth noting that the needle can only be one character, and it is not suitable for Chinese characters.

Example:

$mystring = 'abc';

$findme= 'a';

$pos = strpos($mystring, $findme);



// Note our use of ===.Simply == would not work as expected

// because the position of 'a' was the 0th (first) character.

if ($pos === false) {

echo "The string '$findme' was not found in the string '$mystring'\";

} else {

echo \"The string '$findme' was found in the string '$mystring'\";

echo \" and exists at position $pos\";

}



// We can search for the character, ignoring anything before the offset

$newstring = 'abcdef abcdef';

$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0

?>

Comparison between strpos () and substr_count:

$mystring = "Hello Chris\";

if (substr_count($mystring, \"Hello\") == 0)

echo \"no\";

// same as:

if (strpos($mystring, \"Hello\") === false)

echo \"no\";

?>


Compare the following two codes. Note that numbers are prone to errors and numbers cannot be considered strings.

$val1=123;

$val2="123,456,789\";

if (strpos($val2, $val1)!==false) echo \"matched\";

else echo \"not matched\";

?>

The result is not matched.


$val1=(string)123;

$val2="123,456,789\";

if (strpos($val2, $val1)!==false) echo \"matched\";

else echo \"not matched\";

?>

The result is not matched.

 

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.