Jump to content

Archived

This topic is now archived and is closed to further replies.

robos99

regex syntax problem

Recommended Posts

I've got the following regular expression pattern:
"^[0-9]+$"
to check if a string supplied is a number or not, but i keep getting warnings from php:
Warning: preg_match(): No ending delimiter

I'm kinda confused because isn't the $ the ending delimiter? Maybe I'm missing something.

Share this post


Link to post
Share on other sites
The regex should be:
"/^[0-9]+$/" or "/^\d+$/"

You can also use typcasting and convert the value to a number, using (float) $var, (int) var, settype(), and so on

[code]<?php

echo $_GET['d'] . '<br />';
echo (preg_match("/^\d+$/", $_GET['d'])) ? 'true' : 'false';

?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.