PHP Array Lookup function Summary _php tips

Source: Internet
Author: User

The example of this article summarizes the PHP array lookup function. Share to everyone for your reference. Specifically as follows:

Here are three ways to determine whether a string contains our defined words, which is more suitable for keyword filtering in the address of a message, comment, and so on, with the example code as follows:

Copy Code code as follows:
$crud = Array (' China | | | | China | | | Earth ', ' kelon| | | lerke| | | SB ', ' sesscxx ');
$crud = Join (' | ', $crud);
$crud = Str_replace (' | | | | ', ' | ', $crud);
$pat = "/({$crud})/I";
$txt = ' I know China you are sdfex who!! ';
preg_match/*_all*/($pat, $txt, $matches);
Var_dump ($matches);

Method Two, the code is as follows:
Copy Code code 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 | | | Earth ', ' kelon| | | lerke| | | Ssxb ', ' aaa ');
$test 1 = ' I am Chinese. There are many people in our country. The earth is full of people.-__-!! Good xx's sentence.

Var_dump (Checkcrud ($test 1, $crud));

Method Three, the code is as follows:
Copy Code code as follows:
function Lktest ($v, $keyword) {
foreach ($v as $k) {
if (Strpos ($k, "| | |")! ==false) {
$kelon =explode ("| | |", $K);
To find the maximum value of an array
$b =count ($kelon);
foreach ($kelon as $t) {
echo $t. ' <br> ';
if (Preg_match ('/'. $t. '/I ', $keyword)) {
echo "Sensitive key word";
$a = $a +1;
}
else{
$a = ';
}

}
If the maximum array value is equal to $a, then all | | | The keys are all there.
if ($a = = $b) {
echo "Sensitive key word";
}
}
ElseIf (Preg_match ('/'. $k. '/I ', $keyword)) {
echo "Sensitive key word";
}
}
}

I hope this article will help you with your PHP program design.

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.