PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > regexp to match simple text not working
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
regexp to match simple text not working

Réponse
 
LinkBack Outils de la discussion
Vieux 20/06/2008, 20h08   #1
Raistlin Majere
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut regexp to match simple text not working

// there is

define('VALID_MESSAGE', '/^[a-zA-Z0-9 ,.!?\n]{1,10000}$/');


// there is

function validate($variable, $name, $valid, $name2, $inornot)


// there is

if (!preg_match($valid, $variable)) {
$_SESSION["trying"]="The ".$name." is invalid!";
return false;
}

// it accepts

testtesttesttesttesttest

// it refuses

testtest
testtest
testtest

Why?
  Réponse avec citation
Vieux 20/06/2008, 20h11   #2
Raistlin Majere
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regexp to match simple text not working

// there is too

if (!validate($subject, "subject", VALID_SUBJECT, $subject2, "in")) {
header ("Location: create.php");

On Jun 20, 4:08pm, Raistlin Majere <pedbe...@gmail.com> wrote:
> // there is
>
> define('VALID_MESSAGE', '/^[a-zA-Z0-9 ,.!?\n]{1,10000}$/');
>
> // there is
>
> function validate($variable, $name, $valid, $name2, $inornot)
>
> // there is
>
> if (!preg_match($valid, $variable)) {
> $_SESSION["trying"]="The ".$name." is invalid!";
> return false;
>
> }
>
> // it accepts
>
> testtesttesttesttesttest
>
> // it refuses
>
> testtest
> testtest
> testtest
>
> Why?


  Réponse avec citation
Vieux 20/06/2008, 20h17   #3
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regexp to match simple text not working

On Fri, 20 Jun 2008 21:08:24 +0200, Raistlin Majere <pedbessa@gmail.com>
wrote:
> define('VALID_MESSAGE', '/^[a-zA-Z0-9 ,.!?\n]{1,10000}$/');


> // it accepts
> testtesttesttesttesttest
>
> // it refuses
> testtest
> testtest
> testtest


Most likely, because of \r.
An obvious solution would be:
define('VALID_MESSAGE','/^[a-z0-9,.!?\s]{1,10000}$/i');

Allthough I'd rather use a (mb_)strlen() & '/^[a-z0-9,.!?\s]+$/i' for some
reason. I have no idea wether that's more efficient or not, or even why I
prefer it...
--
Rik Wasmus
....spamrun finished
  Réponse avec citation
Vieux 20/06/2008, 20h43   #4
Raistlin Majere
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regexp to match simple text not working

it still says the message is invalid

On Jun 20, 4:17pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Fri, 20 Jun 2008 21:08:24 +0200, Raistlin Majere <pedbe...@gmail.com>
> wrote:
>
> > define('VALID_MESSAGE', '/^[a-zA-Z0-9 ,.!?\n]{1,10000}$/');
> > // it accepts
> > testtesttesttesttesttest

>
> > // it refuses
> > testtest
> > testtest
> > testtest

>
> Most likely, because of \r.
> An obvious solution would be:
> define('VALID_MESSAGE','/^[a-z0-9,.!?\s]{1,10000}$/i');
>
> Allthough I'd rather use a (mb_)strlen() & '/^[a-z0-9,.!?\s]+$/i' for some
> reason. I have no idea wether that's more efficient or not, or even why I
> prefer it...
> --
> Rik Wasmus
> ...spamrun finished


  Réponse avec citation
Vieux 20/06/2008, 20h50   #5
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regexp to match simple text not working

On Fri, 20 Jun 2008 21:43:05 +0200, Raistlin Majere <pedbessa@gmail.com>
wrote:
> On Jun 20, 4:17pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
>> On Fri, 20 Jun 2008 21:08:24 +0200, Raistlin Majere
>> <pedbe...@gmail.com>
>> wrote:
>>
>> > define('VALID_MESSAGE', '/^[a-zA-Z0-9 ,.!?\n]{1,10000}$/');
>> > // it accepts
>> > testtesttesttesttesttest

>>
>> > // it refuses
>> > testtest
>> > testtest
>> > testtest

>>
>> Most likely, because of \r.
>> An obvious solution would be:
>> define('VALID_MESSAGE','/^[a-z0-9,.!?\s]{1,10000}$/i');


Please don't toppost.

> it still says the message is invalid


Not here it doesn't:
<?php
$variable = 'tasdtasd
asdasd
asdasd
asdasd';
$valid = '/^[a-z0-9,.!?\s]{1,10000}$/';
echo preg_match($valid, $variable) ? 'valid' : 'invalid';
?>
Outputs:valid

If you made sure the right regex & variable arrived at your function, i'd
be interested in this output:
for($i = 0;$i < strlen($variable) ; $i++){
echo $variable[$i].'='.ord($variable[$i])."\n";
}
--
Rik Wasmus
  Réponse avec citation
Vieux 20/06/2008, 20h54   #6
Raistlin Majere
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regexp to match simple text not working

On Jun 20, 4:50pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Fri, 20 Jun 2008 21:43:05 +0200, Raistlin Majere <pedbe...@gmail.com>
> wrote:
>
>
>
> > On Jun 20, 4:17pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> >> On Fri, 20 Jun 2008 21:08:24 +0200, Raistlin Majere
> >> <pedbe...@gmail.com>
> >> wrote:

>
> >> > define('VALID_MESSAGE', '/^[a-zA-Z0-9 ,.!?\n]{1,10000}$/');
> >> > // it accepts
> >> > testtesttesttesttesttest

>
> >> > // it refuses
> >> > testtest
> >> > testtest
> >> > testtest

>
> >> Most likely, because of \r.
> >> An obvious solution would be:
> >> define('VALID_MESSAGE','/^[a-z0-9,.!?\s]{1,10000}$/i');

>
> Please don't toppost.
>
> > it still says the message is invalid

>
> Not here it doesn't:
> <?php
> $variable = 'tasdtasd
> asdasd
> asdasd
> asdasd';
> $valid = '/^[a-z0-9,.!?\s]{1,10000}$/';
> echo preg_match($valid, $variable) ? 'valid' : 'invalid';
> ?>
> Outputs:valid
>
> If you made sure the right regex & variable arrived at your function, i'd
> be interested in this output:
> for($i = 0;$i < strlen($variable) ; $i++){
> echo $variable[$i].'='.ord($variable[$i])."\n";}
>
> --
> Rik Wasmus


the output is

t=116 e=101 s=115 t=116 t=116 e=101 s=115 t=116 t=116 e=101 s=115
t=116 t=116 e=101 s=115 t=116 \=92 r=114 \=92 n=110 t=116 e=101 s=115
t=116 t=116 e=101 s=115 t=116 \=92 r=114 \=92 n=110 t=116 e=101 s=115
t=116 t=116 e=101 s=115 t=116
  Réponse avec citation
Vieux 20/06/2008, 21h07   #7
Raistlin Majere
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regexp to match simple text not working

On Jun 20, 4:54pm, Raistlin Majere <pedbe...@gmail.com> wrote:
> On Jun 20, 4:50pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
>
>
>
> > On Fri, 20 Jun 2008 21:43:05 +0200, Raistlin Majere <pedbe...@gmail.com>
> > wrote:

>
> > > On Jun 20, 4:17pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> > >> On Fri, 20 Jun 2008 21:08:24 +0200, Raistlin Majere
> > >> <pedbe...@gmail.com>
> > >> wrote:

>
> > >> > define('VALID_MESSAGE', '/^[a-zA-Z0-9 ,.!?\n]{1,10000}$/');
> > >> > // it accepts
> > >> > testtesttesttesttesttest

>
> > >> > // it refuses
> > >> > testtest
> > >> > testtest
> > >> > testtest

>
> > >> Most likely, because of \r.
> > >> An obvious solution would be:
> > >> define('VALID_MESSAGE','/^[a-z0-9,.!?\s]{1,10000}$/i');

>
> > Please don't toppost.

>
> > > it still says the message is invalid

>
> > Not here it doesn't:
> > <?php
> > $variable = 'tasdtasd
> > asdasd
> > asdasd
> > asdasd';
> > $valid = '/^[a-z0-9,.!?\s]{1,10000}$/';
> > echo preg_match($valid, $variable) ? 'valid' : 'invalid';
> > ?>
> > Outputs:valid

>
> > If you made sure the right regex & variable arrived at your function, i'd
> > be interested in this output:
> > for($i = 0;$i < strlen($variable) ; $i++){
> > echo $variable[$i].'='.ord($variable[$i])."\n";}

>
> > --
> > Rik Wasmus

>
> the output is
>
> t=116 e=101 s=115 t=116 t=116 e=101 s=115 t=116 t=116 e=101 s=115
> t=116 t=116 e=101 s=115 t=116 \=92 r=114 \=92 n=110 t=116 e=101 s=115
> t=116 t=116 e=101 s=115 t=116 \=92 r=114 \=92 n=110 t=116 e=101 s=115
> t=116 t=116 e=101 s=115 t=116


it really looks like the problem is not with the regexp, but with the
code

but what is the problem???
  Réponse avec citation
Vieux 20/06/2008, 21h08   #8
Raistlin Majere
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regexp to match simple text not working

On Jun 20, 4:50pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Fri, 20 Jun 2008 21:43:05 +0200, Raistlin Majere <pedbe...@gmail.com>
> wrote:
>
>
>
> > On Jun 20, 4:17pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> >> On Fri, 20 Jun 2008 21:08:24 +0200, Raistlin Majere
> >> <pedbe...@gmail.com>
> >> wrote:

>
> >> > define('VALID_MESSAGE', '/^[a-zA-Z0-9 ,.!?\n]{1,10000}$/');
> >> > // it accepts
> >> > testtesttesttesttesttest

>
> >> > // it refuses
> >> > testtest
> >> > testtest
> >> > testtest

>
> >> Most likely, because of \r.
> >> An obvious solution would be:
> >> define('VALID_MESSAGE','/^[a-z0-9,.!?\s]{1,10000}$/i');

>
> Please don't toppost.
>
> > it still says the message is invalid

>
> Not here it doesn't:
> <?php
> $variable = 'tasdtasd
> asdasd
> asdasd
> asdasd';
> $valid = '/^[a-z0-9,.!?\s]{1,10000}$/';
> echo preg_match($valid, $variable) ? 'valid' : 'invalid';
> ?>
> Outputs:valid
>
> If you made sure the right regex & variable arrived at your function, i'd
> be interested in this output:
> for($i = 0;$i < strlen($variable) ; $i++){
> echo $variable[$i].'='.ord($variable[$i])."\n";}
>
> --
> Rik Wasmus


It really looks like the problem is not with the regexp, but with the
rest of the code, but what is the problem???
  Réponse avec citation
Vieux 20/06/2008, 21h14   #9
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regexp to match simple text not working

On Fri, 20 Jun 2008 21:54:57 +0200, Raistlin Majere <pedbessa@gmail.com>
wrote:

> On Jun 20, 4:50pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
>> On Fri, 20 Jun 2008 21:43:05 +0200, Raistlin Majere
>> <pedbe...@gmail.com>
>> wrote:
>>
>>
>>
>> > On Jun 20, 4:17pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
>> >> On Fri, 20 Jun 2008 21:08:24 +0200, Raistlin Majere
>> >> <pedbe...@gmail.com>
>> >> wrote:

>>
>> >> > define('VALID_MESSAGE', '/^[a-zA-Z0-9 ,.!?\n]{1,10000}$/');
>> >> > // it accepts
>> >> > testtesttesttesttesttest

>>
>> >> > // it refuses
>> >> > testtest
>> >> > testtest
>> >> > testtest

>>
>> >> Most likely, because of \r.
>> >> An obvious solution would be:
>> >> define('VALID_MESSAGE','/^[a-z0-9,.!?\s]{1,10000}$/i');

>>
>> Please don't toppost.
>>
>> > it still says the message is invalid

>>
>> Not here it doesn't:
>> <?php
>> $variable = 'tasdtasd
>> asdasd
>> asdasd
>> asdasd';
>> $valid = '/^[a-z0-9,.!?\s]{1,10000}$/';
>> echo preg_match($valid, $variable) ? 'valid' : 'invalid';
>> ?>
>> Outputs:valid
>>
>> If you made sure the right regex & variable arrived at your function,
>> i'd
>> be interested in this output:
>> for($i = 0;$i < strlen($variable) ; $i++){
>> echo $variable[$i].'='.ord($variable[$i])."\n";}


> the output is
>
> ... \=92...


'\' is not valid, some code is escaping the \r & \n characters in your
$variable when you don't expect it. '\n' != "\n" Not something
addslashes() or magic_quotes would do, so the usual suspects for incorrect
escaping do not apply. Perhaps a premature mysql_(real_)escape_string?
Trace it back I'd say, examine the route your $variable goes, and aat what
point the newline character stops being a newline character.
--
Rik Wasmus
....spamrun finished
  Réponse avec citation
Vieux 20/06/2008, 22h36   #10
Raistlin Majere
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regexp to match simple text not working

On Jun 20, 5:14pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Fri, 20 Jun 2008 21:54:57 +0200, Raistlin Majere <pedbe...@gmail.com>
> wrote:
>
>
>
> > On Jun 20, 4:50pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> >> On Fri, 20 Jun 2008 21:43:05 +0200, Raistlin Majere
> >> <pedbe...@gmail.com>
> >> wrote:

>
> >> > On Jun 20, 4:17pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> >> >> On Fri, 20 Jun 2008 21:08:24 +0200, Raistlin Majere
> >> >> <pedbe...@gmail.com>
> >> >> wrote:

>
> >> >> > define('VALID_MESSAGE', '/^[a-zA-Z0-9 ,.!?\n]{1,10000}$/');
> >> >> > // it accepts
> >> >> > testtesttesttesttesttest

>
> >> >> > // it refuses
> >> >> > testtest
> >> >> > testtest
> >> >> > testtest

>
> >> >> Most likely, because of \r.
> >> >> An obvious solution would be:
> >> >> define('VALID_MESSAGE','/^[a-z0-9,.!?\s]{1,10000}$/i');

>
> >> Please don't toppost.

>
> >> > it still says the message is invalid

>
> >> Not here it doesn't:
> >> <?php
> >> $variable = 'tasdtasd
> >> asdasd
> >> asdasd
> >> asdasd';
> >> $valid = '/^[a-z0-9,.!?\s]{1,10000}$/';
> >> echo preg_match($valid, $variable) ? 'valid' : 'invalid';
> >> ?>
> >> Outputs:valid

>
> >> If you made sure the right regex & variable arrived at your function,
> >> i'd
> >> be interested in this output:
> >> for($i = 0;$i < strlen($variable) ; $i++){
> >> echo $variable[$i].'='.ord($variable[$i])."\n";}

> > the output is

>
> > ... \=92...

>
> '\' is not valid, some code is escaping the \r & \n characters in your
> $variable when you don't expect it. '\n' != "\n" Not something
> addslashes() or magic_quotes would do, so the usual suspects for incorrect
> escaping do not apply. Perhaps a premature mysql_(real_)escape_string?
> Trace it back I'd say, examine the route your $variable goes, and aat what
> point the newline character stops being a newline character.
> --
> Rik Wasmus
> ...spamrun finished


IT WORKED!!! It was a premature mysql_real_escape_string indeed! Thank
you very much!
  Réponse avec citation
Vieux 22/06/2008, 06h49   #11
Baris-C
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regexp to match simple text not working

On Jun 20, 10:08pm, Raistlin Majere <pedbe...@gmail.com> wrote:
> // there is
>
> define('VALID_MESSAGE', '/^[a-zA-Z0-9 ,.!?\n]{1,10000}$/');
>
> // there is
>
> function validate($variable, $name, $valid, $name2, $inornot)
>
> // there is
>
> if (!preg_match($valid, $variable)) {
> $_SESSION["trying"]="The ".$name." is invalid!";
> return false;
>
> }
>
> // it accepts
>
> testtesttesttesttesttest
>
> // it refuses
>
> testtest
> testtest
> testtest
>
> Why?

YOU NEED TO ESCAPE SPECIAL CHARACTERS LIKE DOTS . AND DASHES - \.
\- AND ALSO \$ \^ \[ \] \{ ETC. THEY GO ON IF YOU ARE SEARCHING FIR
THEM
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h09.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,22669 seconds with 19 queries