PDA

View Full Version : preg_match pattern question


dealmaker
02-03-2006, 08:01 AM
Hi,
I got the following error, and I don't know why. I try to search characters "@#^&-_+=\:;/," at the end of a string. How do I fix it? Many thanks.

Parse error: parse error, unexpected '\"'


class myclass {
const pattern = "/[@#^&\-_+=\\:;/,]$/";
function func($data) {
if(preg_match(self::pattern, $data)) // parse error
echo 'match';
}
}

$var = myclass();
$var->func('12345');

fci
02-03-2006, 02:20 PM
just needed some escaping and I removed two of the blackslashes
const pattern = "/[@#^&\-_+=:;\/,]$/";

dealmaker
02-03-2006, 05:58 PM
I still got this error message:
Parse error: parse error, unexpected '\"'

How to fix it?

just needed some escaping and I removed two of the blackslashes
const pattern = "/[@#^&\-_+=:;\/,]$/";

fci
02-03-2006, 06:21 PM
mm, ah I think it was a result of the double quotes on my part (I had tested using single quotes)
const pattern = '/[@#^&-_+=:;\\/,]$/';

dealmaker
02-03-2006, 07:18 PM
why double quotes make it doesn't work? I thought that whenever you have a escape character for special characters, you need double quotes. So why not in this case?

mm, ah I think it was a result of the double quotes on my part (I had tested using single quotes)
const pattern = '/[@#^&-_+=:;\\/,]$/';

fci
02-03-2006, 07:31 PM
double quotes means you have to escape more things. I rarely use double quotes. you can use special characters in single quotes as much as you want (well, escape for single quotes which just need a backslash).