Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

filter_var problems

Explorer ,
Jul 09, 2009 Jul 09, 2009

Copy link to clipboard

Copied

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.

TOPICS
Server side applications

Views

687
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 10, 2009 Jul 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() get

...

Votes

Translate
LEGEND ,
Jul 10, 2009 Jul 10, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 10, 2009 Jul 10, 2009

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines