Technical Discussion
  >> Web Design / HTML / Web hosting Forum


Register (or login) on our website and you will not see this ad.


These posts have been archived and can no longer be replied to or modified.
Pages in this thread: 1 | 2 | (show all)   Print Thread
Standard User cheshire_man
(experienced) Thu 09-Sep-10 16:02:15
Print Post

PHP string pattern


[link to this post]
 
I've come across the following string pattern in some PHP code and am trying to understand exactly what it means. I know it's used in an ereg() which should be replaced by preg_match(), that's another job.

PHP
1
ereg("^[A-Za-z0-9'\r\n\/,\.#()!?�$*+= -]+$",$value)


As far as I can see that's checking to see if any (1 or more) of the specified characters exist in $value. The characters being checked for being

any upper or lower case letter
any digit
' [apostrophe]
/ [forward slash]
, [comma]
. [period or full stop]
# [hash]
( [open round bracket]
) [close round bracket]
! [exclamation mark or shriek]
? [question mark]
£ [pound sign]
$ [dollar sign]
* [asterisk]
+ [plus sign]
= [equals sign]
[space]
- [minus sign or hyphen]

I have no idea what the � is there for, perhaps it's some sort of typo. (It's immediately before the apostrophe but has been removed by the forum code.)

Also I cannot understand the use of escape r and escape n.

Have I understood the pattern correctly?

Any thoughts on the � and the escape r and n ?

Tony

Edited by cheshire_man (Thu 09-Sep-10 16:03:19)

Standard User ukwiz
(fountain of knowledge) Thu 09-Sep-10 16:06:47
Print Post

Re: PHP string pattern


[re: cheshire_man] [link to this post]
 
\r\n are carriage return, linefeed

David

O2 Pro - Unlimited, cheap, some support
Standard User micksharpe
(eat-sleep-adslguide) Thu 09-Sep-10 16:18:00
Print Post

Re: PHP string pattern


[re: cheshire_man] [link to this post]
 
Your understanding is correct.

For preg_match, you will need:
Text
1
preg_match("/^[A-Za-z0-9'\r\n\/,\.#()!?�$*+= -]+$/",$value)

(the string contains a regexp including the // delimiters)

__________________________________________________________________________________________
"Windows Vista Sir?"
"No thanks, I'd rather shove wasps up my nostrils!" .
Mick's Blog | Greasemonkey scripts

Edited by micksharpe (Thu 09-Sep-10 16:18:57)


Register (or login) on our website and you will not see this ad.

Standard User ukwiz
(fountain of knowledge) Thu 09-Sep-10 16:24:00
Print Post

Re: PHP string pattern


[re: cheshire_man] [link to this post]
 
Do you mean greek beta, or german double s (not that I know what either of them would be in this context)

David

O2 Pro - Unlimited, cheap, some support
Standard User cheshire_man
(experienced) Thu 09-Sep-10 16:24:26
Print Post

Re: PHP string pattern


[re: ukwiz] [link to this post]
 
I was aware of that in the context of generating HTML from PHP. But you've made me wonder - does that mean that, for example, if the ereg() was being used to check data input in an HTML form and the user had used the return/enter key \r\n would need to be permitted as they'd be in the data that comes in from the <textarea> ?

Tony
Standard User cheshire_man
(experienced) Thu 09-Sep-10 16:27:05
Print Post

Re: PHP string pattern


[re: ukwiz] [link to this post]
 
German double s - actually it's hex DF (dec.223) which I think is the German double s

Tony
Standard User deleted
(deleted) Thu 09-Sep-10 16:30:43
Print Post

Re: PHP string pattern


[re: cheshire_man] [link to this post]
 
I don't think you have accounted for the initial caret, which is saying that the character being matched must be the first one in $value, or the "+$" at the end.

+ matches any single character and $ matches the end of the string. Hence (I think) the pattern is looking to match $value consisting of one of the characters that you mentioned (or CR or LF) followed by any one character. So just a two-character string.

I'm just a little bit worried about the double quotes and the effect this would have on, in particular, the two $ characters. It's more usual to see regular expressions like this in single quotes.
Standard User cheshire_man
(experienced) Thu 09-Sep-10 16:37:25
Print Post

Re: PHP string pattern


[re: deleted] [link to this post]
 
Take your point (I think) about the start ^ and the end $ although does the use of the [ ] brackets have an effect?

I understood the + to mean one or more of the literal characters enclosed in the [ ] brackets.

The double quotes are needed - aren't they? - as there is a single quote inside them, changing the double quotes to single quotes would cause a syntax problem, wouldn't it?

Tony
Standard User deleted
(deleted) Thu 09-Sep-10 16:46:13
Print Post

Re: PHP string pattern


[re: cheshire_man] [link to this post]
 
Sorry - you are correct about the plus sign. So I think that $value must be just a single character - one from the set in [].

I'm still a little confused about the fact the some characters that I would expect to be escaped (for example the first $ and the ') aren't, but that may be because I am more used to Perl than PHP. Perhaps the fact that these characters are inside the [] has some significance here. I always need several goes at constructing a regular expression before I arrive at what I intend! A little experimentation might make things clearer in this case.
Standard User ukwiz
(fountain of knowledge) Thu 09-Sep-10 16:53:16
Print Post

Re: PHP string pattern


[re: cheshire_man] [link to this post]
 
In reply to a post by cheshire_man:
Take your point (I think) about the start ^ and the end $ although does the use of the [ ] brackets have an effect?
I understood the + to mean one or more of the literal characters enclosed in the [ ] brackets.

The ^ and $ are from the beginning and to the end, so you are matching one or more (the +) of the characters in square brackets
The double quotes are needed - aren't they? - as there is a single quote inside them, changing the double quotes to single quotes would cause a syntax problem, wouldn't it?
yes, they are needed

David

O2 Pro - Unlimited, cheap, some support
Pages in this thread: 1 | 2 | (show all)   Print Thread

Jump to