PHP Find String Learning Note _php Tutorial

Source: Internet
Author: User
This article describes some methods of finding strings in PHP, including: Finding a string, finding the number of occurrences of a string, finding a string position, three ways to find a string, and a friend you need to know to access the reference.

PHP string Lookup characters can be implemented through the STRSTR (), STRRCHR () functions, and the two functions are structured as follows:

Strstr ($haystack, $needle)
STRRCHR ($haystack, $needle)

$haystack represents a female string, $needle represents the character to find
The Strstr () function is used to find the position of the first occurrence of a character and returns the part from which the position begins to the end of the parent string;

The STRRCHR () function is used to find the position of the last occurrence of a character, and returns the portion from this position to the end of the parent string.

Instance:

The code is as follows Copy Code
$a = "P";
$b = "echo is PHP function, p is HTML tag";
$str 1=strstr ($b, $a);
if ($str 1)
echo $str 1. "
";
else echo "Could not find P". "
";

$str 2=STRRCHR ($b, $a);
if ($str 2)
echo $str 2;
else echo "Cannot find P";
?>

PHP string occurrences can be found through the Substr_count () function, the structure of the form:

Substr_count ($haystack, $needle [, $offset [, $length]])
$haystack represents a female string, $needl represents the character to find
$offset represents the starting point for the lookup, $length the length of the lookup, which is an optional parameter
Instance:

The code is as follows Copy Code

$str = "This is a test";
Echo Substr_count ($str, ' is '). '
';
Echo Substr_count ($str, ' is ', 3). '
';
Echo Substr_count ($str, ' is ', 3, 3). '
';
?>

PHP Lookup string Locations can be implemented using the Strrpos (), Strpos () function.

Find the last Location
Using the function Strrpos (), the structure is as follows:

Strrpos ($haystack, $needle [, $offset])
The parameter $needle can only be one character, not a string. If a string is supplied, only the first character in the string is taken, and the other characters are invalid;
Optional parameter $offset sets the length of the lookup string.
Example 1:

The code is as follows Copy Code

$text = "PHP";
$str 1=strrpos ($text, "P");
if ($str 1)
echo "found the character ' P '".
";
else echo "Cannot find the character ' P '".
";
$str 2=strrpos ($stxt, "Q");
if ($str 2)
echo "found the character ' Q '".
";
else echo "Cannot find the character ' Q '".
";
?>

Find the location for the first time
With the Strpos () function, and the function Strrpo () Only one letter, the function is very different. The $needle parameter of the Strpos () function allows the use of a string, and finds the first occurrence of the string in $haystack, rather than the last time.

Instance:

The code is as follows Copy Code

$text = "One of the differences between the PHP language and the ASP language: PHP is more stable and secure";
$str 1=strrpos ($text, "PHP");
if ($str 1)
echo "found the string ' php ', the location is now: $str 1". "
";
else echo "Could not find the string ' php '".
";
$str 2=strpos ($stxt, "PHP");
if ($str 2)
echo "found the string ' php '".
";
else echo "Could not find the string ' php '".
";
?>

http://www.bkjia.com/PHPjc/628834.html www.bkjia.com true http://www.bkjia.com/PHPjc/628834.html techarticle This article describes some methods of finding strings in PHP, including: Finding a string, finding the number of occurrences of a string, finding a string position, three things about string lookups, ...

  • 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.