0

Can anyone give me a solution for the below lines of code:

 $filteredArray = array_filter($wordArray, function ($x) {
    return !preg_match("/^(.|a|an|and|the|this|at|in|or|of|is|for|to|em|com|be
       |with|href|me|rt|by|np|http|www)$/x", $x);
 });

This line not producing any error in my localhost using XAMPP, but the same line is showing an error in my nginx server (online).

What can I do for this. Why this is showing?

4
  • Parse error: syntax error, unexpected T_FUNCTION in /hermes/bosweb26c/b2656/nf.theonlytutorialscom/agurchand/solutions/gootwit/test.php on line 10 Commented Apr 26, 2012 at 18:28
  • 1
    What version of PHP is on your nginx server? The anonymous function is only valid in 5.3 Commented Apr 26, 2012 at 18:29
  • Syntax to do it right: callback function in array filter Commented Apr 26, 2012 at 18:30
  • thanks michael. php version is 5.2.12 Commented Apr 26, 2012 at 18:32

2 Answers 2

2

Sounds like your server may not be running PHP 5.3 or later, which is needed to support anonymous functions. Instead, you can create the function and pass it as a callback function string to array_filter().

function wordFilter($x) {
  return !preg_match("/^(.|a|an|and|the|this|at|in|or|of|is|for|to|em|com|be
    |with|href|me|rt|by|np|http|www)$/x", $x);
}

$filtered_array = array_filter($wordArray, 'wordFilter');
Sign up to request clarification or add additional context in comments.

Comments

0

Your version of php on the nginx server is too old to use anonymous functions (closures -- at least PHP 5.3) is required. You can use create_function to create a function in older versions, and that still works in newer versions as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.