Skip to main content
Inspiring
July 9, 2009
Answered

filter_var problems

  • July 9, 2009
  • 1 reply
  • 768 views

Using CS4 and PHP 5.2, I have the following php code:

        $mailarray =  explode("\n", trim($formvars['manual']));
        $tolist['email'] = array();
        foreach ($mailarray as $value)
        {
            if (filter_var($value, FILTER_VALIDATE_EMAIL))
                $tolist['email'][] = $value;
        }

$formvars['manual'] comes from a form textarea and is a list of email addresses entered as one per line - and I want to ignore lines that contain invalid emails. My basic problem is that the $tolist array never gets populated (looks like filter_var always returns false). I am sure I'm just making a stupid mistake..?

Ed

PS CS4 doesn't show filter_var as a 'blue' function in the code window, so I assume doesn't recognise it (I wonder how one can tweak that), but my version of PHP definitely does.

This topic has been closed for replies.
Correct answer David_Powers

Interesting little problem there. Had me scratching my head for a while, but using echo and print_r() to observe what was happening at each stage eventually sparked a light.

Change this:

if (filter_var($value, FILTER_VALIDATE_EMAIL))

to this:

if (filter_var(trim($value), FILTER_VALIDATE_EMAIL))

and it will work.

Windows adds a carriage return as well as a newline character. Consequently, all except the last item was being rejected, a return character being invalid in an email address. Using trim() gets rid of the rogue character that wasn't stripped when using explode().

As for the code hint not being there, you can add it manually to the code hints page in the Dreamweaver configuration. The file is C:\Program Files\Adobe\Adobe Dreamweaver CS4\configuration\CodeHints\CodeHints.xml. On a Mac, it's in a similar location in the Applications folder.

1 reply

David_Powers
David_PowersCorrect answer
Inspiring
July 10, 2009

Interesting little problem there. Had me scratching my head for a while, but using echo and print_r() to observe what was happening at each stage eventually sparked a light.

Change this:

if (filter_var($value, FILTER_VALIDATE_EMAIL))

to this:

if (filter_var(trim($value), FILTER_VALIDATE_EMAIL))

and it will work.

Windows adds a carriage return as well as a newline character. Consequently, all except the last item was being rejected, a return character being invalid in an email address. Using trim() gets rid of the rogue character that wasn't stripped when using explode().

As for the code hint not being there, you can add it manually to the code hints page in the Dreamweaver configuration. The file is C:\Program Files\Adobe\Adobe Dreamweaver CS4\configuration\CodeHints\CodeHints.xml. On a Mac, it's in a similar location in the Applications folder.

EdP21Author
Inspiring
July 10, 2009

David you are an absolute genius - many thanks!

By the way, the code hint was indeed there, but it looks like Adobe has forgotten to put quite a few of the new functions in the PHP.xml file (found in C:\Program Files\Adobe\Adobe Dreamweaver CS4\configuration\CodeColoring on Windows). Adding <keyword>filter_var</keyword> has turned it nicely blue.

Ed