CoderZone.org
Pages: 1 « previous     next »
  Print  
Author Topic: [SOLVED] validation question  (Read 8416 times) Bookmark and Share
phpMan2010
Newbie
*
Posts: 32



View Profile
« on: Dec 05, 2010, 07:51:15 am »

i'm getting some XML data

Code:  
Highlight Mode: (XML)
  1. <datafield datatype="string"><name>first_name</name><value>ÀJohnÃÀ</value></datafield>
 

and validating it with a regexp

Code:  
^[\w\ \.\-]{1,20}$

in this code

Code:  
Highlight Mode: (PHP)
  1.                        $sInput = $value;
  2.                        $sRegExp = '^[\w\ \.\-]{1,20}$';
  3.                        $bMatch=mb_ereg($sRegExp,$sInput);
  4. file_put_contents('/tmp/x.out','Validating '.$name.' = '.$value.' against '.$sRegExp.(($bMatch)?'okay':'invalid').PHP_EOL,FILE_APPEND);
  5.                        if (!$bMatch)
  6.                                return $name;
 

which should validate, but it doesn't!

Validating first_name = ÀJohnÃÀ against ^[\w\ \.\-]{1,20}$ invalid

what am i doing wrong?
« Last Edit: Dec 08, 2010, 11:51:06 am by Mr. Crane » Logged
Cruise Elroy
Newbie
*
Posts: 14



View Profile
« Reply #1 on: Dec 05, 2010, 09:16:04 am »

My first guess would be that character encoding might be coming into play here.

Logged
wheels
Jr. Member
*****
Posts: 62



View Profile
« Reply #2 on: Dec 05, 2010, 10:42:48 am »

The blue letters aren't valid for the \w

ÃJohnÃÃ
Logged
phpMan2010
Newbie
*
Posts: 32



View Profile
« Reply #3 on: Dec 05, 2010, 12:36:29 pm »

tried the new string, but it still doesn't work
Logged
wheels
Jr. Member
*****
Posts: 62



View Profile
« Reply #4 on: Dec 05, 2010, 05:10:05 pm »

Cruise has a good idea - can you post the first few lines of the XML?
Logged
phpMan2010
Newbie
*
Posts: 32



View Profile
« Reply #5 on: Dec 06, 2010, 08:52:23 am »

Code:  
Highlight Mode: (XML)
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <request>
  3. <header>
 
Logged
cuberat
Newbie
*
Posts: 40


View Profile
« Reply #6 on: Dec 06, 2010, 11:54:56 am »

This worked okay for me ...

Code:  
Highlight Mode: (PHP)
  1. <?php
  2. mb_detect_order('UTF-8');
  3. mb_language('uni');
  4. $sRegExp="^[\w\ \.\-]{1,20}$";
  5. $sInput="ÃJohnÃÃ";
  6. $bMatch=mb_ereg($sRegExp,$sInput);
  7. var_dump($bMatch);
 


you might want to try adding the setup code into your stuff ...
Logged
phpMan2010
Newbie
*
Posts: 32



View Profile
« Reply #7 on: Dec 06, 2010, 04:02:23 pm »

yeah - that works

on both the command line and using a browser

but if i submit the data using the form, it doesn't work
Logged
cuberat
Newbie
*
Posts: 40


View Profile
« Reply #8 on: Dec 06, 2010, 08:10:14 pm »

Check the form for a META tag, like this:

Code:  
Highlight Mode: (HTML)
  1. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 

If it isn't UTF-8, the regexes will fail.
Logged
phpMan2010
Newbie
*
Posts: 32



View Profile
« Reply #9 on: Dec 08, 2010, 06:17:09 am »

the form that was sending the data was using iso-8859-1

Cruise was right

thanks to all!
« Last Edit: Dec 08, 2010, 06:21:16 am by phpMan2010 » Logged
Tags:
Pages: 1
  Print  
 
Jump to: