Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 01-18-2006, 03:34 PM   #1 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
preg_match problems

Hey guys... Been a while again...

I'm having a problem using the preg_match function in PHP 4. Basically, I'm checking to see if any of the following characters appear in whatever the user enters:

.*$?"/|\:<>

And then using an if statement to do whatever I want...

What exactly would be my if statement with the above characters and preg_match?

I'm thinking of something along the lines of:

PHP Code:
if( preg_match(/^($)/, $url2) ) {
// stuff here...

Not sure how to include those characters in the function...

Thanks.
gamehead200 is offline   Reply With Quote
Old 01-18-2006, 03:59 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,487
sde is on a distinguished road
some of those characters are regular expression characters and need to be escaped with a backslash. this should work.

PHP Code:
if ( preg_match("/[\.\*$?\"\/\|\\:<>]/"$text) ) {
    echo 
"match";
} else {
    echo 
"no match";

sde is offline   Reply With Quote
Old 01-18-2006, 04:21 PM   #3 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
Sweet!! It finally works and everything makes much more sense now!

Thanks!
gamehead200 is offline   Reply With Quote
Old 01-19-2006, 02:51 AM   #4 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 673
DJMaze is on a distinguished road
$ and ? also need to be properly backslashed due to the double quoting
On single quoting only the ?

This is because the following chars are special in
regex: .*?|
php: $

SDE uses "/[\.\*$?\"\/\|\\:<>]/" which makes / also a special char
Another way is bash: "#[\.\*$?\"/\|\\:<>]#"

preg_match("/[\.\*\$\?\"\/\|\\:<>]/"
preg_match('/[\.\*$\?"\/\|\\:<>]/'
preg_match('#[\.\*$\?"/\|\\:<>]#"

As you can see there are several solutions to a single problem, so use the one you like most.
DJMaze is offline   Reply With Quote
Old 01-19-2006, 09:22 AM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,487
sde is on a distinguished road
good insight. i think i got away with it because the characters i neglected to escape are inside brackets. i did test for every character and my example does work fine.
sde is offline   Reply With Quote
Old 01-21-2006, 08:54 PM   #6 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
If you have characters in a [] inside a regular expression, you are declaring a character class. Within a character class, the only characters that are special are '^' (at the beginning to negate), '-' (for ranges), '[' , ']' and the trailing delimeter ('/' in sde's case).
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 01-22-2006, 07:21 AM   #7 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 673
DJMaze is on a distinguished road
Not entirely since [.*] is legit for any char so if you need those chars you need [\,\*]

The $ is a php case since "$foo" is as var $foo but '$foo' isn't, so you use "\$foo" or '$foo' to keep your coding everywhere the same and that prevents errors.

Example
PHP Code:
$string = 'You need $foo';
$foo = 'money';
preg_replace('#$foo#', '<?php echo $foo?>', $string);
eval($string);
preg_replace("#$foo#", "<?php echo $foo?>", $string);
eval($string);
preg_replace("#\$foo#", "<?php echo $foo?>", $string);
eval($string);
Outputs:

You need money
You need $foo
You need money
DJMaze is offline   Reply With Quote
Old 01-22-2006, 01:25 PM   #8 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
No, [.*] means match a literal '.' or a literal '*' [.]* means match 0 or more '.'s .
You are correct about interpolating $variables into a double quoted string.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Similar problems..over and over! whiteFLAME PHP 6 09-02-2005 08:58 AM
cin.getline problems mconn86 Standard C, C++ 5 04-14-2005 01:02 PM
Problems debugging visual c++ in vs.net 2003 Engineer MS Technologies ( ASP, VB, C#, .NET ) 2 07-20-2004 12:19 PM
no dial tone problems fabiopao Linux / BSD / OS X 4 02-23-2003 10:51 AM
I have problems... anon Lounge 4 01-12-2003 07:47 PM


All times are GMT -8. The time now is 05:01 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting