Php array lookup Function Summary

Source: Internet
Author: User
This article mainly introduces php array lookup functions and summarizes three commonly used array lookup functions that can be used for keyword filtering. it is a very practical technique, for more information about php array lookup functions, see the examples in this article. Share it with you for your reference. The details are as follows:

Here, we provide three methods to determine whether a string contains the words we have defined. this method is suitable for keyword filtering at the message, comment, and other addresses. The example code is as follows:

The code is as follows:

$ Crud = array ('China | China | ', 'kelon | lerke | sb', 'sesscxx ');
$ Crud = join ('|', $ crud );
$ Crud = str_replace ('|', '|', $ crud );
$ Pat = "/({$ crud})/I ";
$ Txt = 'I know China, who are you sdfex !! ';
Preg_match/* _ all */($ pat, $ txt, $ matches );
Var_dump ($ matches );


Method 2: The code is as follows:

The code is as follows:

Function checkcrud ($ str, $ crud)
{
If (is_array ($ crud )&&! Emptyempty ($ crud ))
{
Foreach ($ crud as $ value)
{
If (strpos ($ value, '| ')! = False)
{
$ Cruds = explode ('|', $ value );
$ Num = count ($ cruds );
$ Check = 0;
Foreach ($ cruds as $ val)
{
If (strpos ($ str, $ val )! = False)
{
$ Check ++;
}
}
If ($ check = $ num)
{
Return true;
}
}
Else
{
If (strpos ($ str, $ value )! = False)
{
Return true;
}
}
}
Return false;
}
}
$ Crud = array ('China | China | ', 'kelon | lerke | ssxb', 'AAA ');
$ Test1 = 'I am a chinese. There are many Chinese people. There are all people on the earth .-__-!! Xx sentence .';

Var_dump (checkcrud ($ test1, $ crud ));


Method 3: The code is as follows:

The code is as follows:

Function lktest ($ v, $ keyword ){
Foreach ($ v as $ k ){
If (strpos ($ k, "| ")! = False ){
$ Kelon = explode ("|", $ k );
// Calculate the maximum value of the array
$ B = count ($ kelon );
Foreach ($ kelon as $ t ){
// Echo $ t .'
';
If (preg_match ('/'. $ t. '/I', $ keyword )){
// Echo "sensitive keywords ";
$ A = $ a + 1;
}
Else {
$ A = '';
}

}
// If the maximum value of the array is equal to $ A, all | keywords are displayed.
If ($ a = $ B ){
Echo "sensitive keywords ";
}
}
Elseif (preg_match ('/'. $ k. '/I', $ keyword )){
Echo "sensitive keywords ";
}
}
}

I hope this article will help you with PHP programming.

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.