PHP determines whether to include a string ____php

Source: Internet
Author: User
Tags explode

Original: http://www.oschina.net/code/snippet_98890_27459

PHP to determine the inclusion of a string, you can use PHP's built-in function Strstr,strpos,stristr direct judgment. You can also write a judgment function by using the function of explode:

1. [use of code]STRSTR and Stristr

Strstr: Returns a string from the start of the judged character to the end, which is not included if there is no return value. STRISTR: It's exactly the same way as strstr. The only difference is that STRISTR is case-insensitive.

$email = ' user@example.com ';
$domain = Strstr ($email, ' @ ');
echo $domain;
Prints @example. com
2. [Use of code]strposStrpos: Returns a Boolean value. False and true do not have to say much. Use "= = =" to judge. Strpos is faster in execution than two functions above, and Strpos has a parameter that specifies the location of the decision, but the default is null. It means to judge the whole string. The disadvantage is that the support for Chinese is not good. The PHP judgment string contains the following code:
$STR = ' abc ';
$needle = ' a ';
$pos = Strpos ($str, $needle);
3. [Use of Code]explode
function Checkstr ($str) {
    $needle = "a";//To determine whether to include a character
    $tmparray = explode ($needle, $str);
    if (count ($tmparray) >1) {return
    true;
    } else{return
    false;
 }}

4.[Code] in_array-Check to see if a value example exists in the array
1. In_array () example
<?php$os= Array ("Mac", "NT", "Irix", "Linux"); if (In_array ("Irix", $os)) {echo
"Got Irix";}
if (
  in_array ("Mac", $os)) {echo "Got mac";}
? >
the second condition fails because the In_array () is case-sensitive, so the above program is shown as: Got Irix
 
 
2. In_array () Strict type checking example
<?php$a= array (' 1.10 ', 12.4, 1.13); if (In_array (' 12.4 ', $a, true)) {echo "' 12.4 ' found with strict check\n";}
if (
in_array (1.13, $a, true)) 
{echo "1.13 found with strict check\n";
> The
example will output:
1.13 found with strict check
 
 
3. In_array () using an array as needle
<?php$a
 
= Array (array (' P ', ' h '), Array (' P ', ' r '), ' o '); if (
in_array (' P ', ' h '), $a)) {echo '
' ph ' is found\n ';}
if (
In_array (' f ', ' I '), $a)) {echo '
' fi ' was found\n ';}
if (
in_array (' o ', $a)) {echo
"' O ' is found\n";}
? > The
 
above example will output:
' ph '
was found ' o ' is found
 
array_search-searches the array for a given value and returns the corresponding key name if successful

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.