Regular expression variable

Why doesn’t the $filter which is inserted in the code and which is supposed to filter out rows with a value not work? If you insert directly, then it filters out, but if through a variable, then it doesn’t.


Answer 1, authority 100%

Because you are doing heresy O_O

There are no “compiler directives” in php, you can’t use constants and variables as part of boolean expressions.

Even if you mess something up there, "& !preg_match(\"/title=''/\", $str)"will just return TRUE as “non-empty string” .

The maximum you can do is:

function myFilter($str) { return !preg_match("/title=''/", $str); }
$filter = "myFilter";
/* code */
if(!preg_match('/alt=" "/', $str) && !preg_match('/alt=" "/', $str) && !preg_match('/alt=" "/', $str) && $filter($str))
/* code */

Answer 2

try this:

function myFilter($str) {
$filter = array("title=''",
        'alt=" "',
        'alt=" "'
        );//      .
foreach($filter as $r){
    if(!strpos($str,$r))return false;
}
     return true;
}
/*code*/
if(myFilter($str))
/*code*/