Angelo Zanetti wrote:
Hi guys
Been tryin to figure out regex and have found some tutorials but some
have made things clear and others have confused me.
Anyway for a simple query, if I just wanted to check that a variable has
only text and numeric characters would I do something like this
(i want it to fail if it finds a symbol eg: + - { " etc...):
echo "REG result: " . preg_match('/[a-zA-Z0-9]*/', '{d-fg');
you're on the right path, whats needed is start and end [string] delimiters
in the regexp (note I used a different regexp delimiter, '#', which is irrelevant):
echo "REG 1 result: ", preg_match("#[a-zA-Z0-9]*#", "{d-fg"), "\n",
"REG 2 result: ", preg_match("#^[a-zA-Z0-9]*$#", "{d-fg"), "\n";
the magic characters are '^' andf '$' as explained in more detail here:
http://php.net/manual/en/reference.pcre.pattern.syntax.php
<quote>
^
assert start of subject (or line, in multiline mode)
$
assert end of subject (or line, in multiline mode)
</quote>
however this resolves to true because there are normal characters
(alphabetical characters) so how do I make the expression fail because
of the { and - characters in the string?
You can also list which characters you DON'T want -- just use a '^' as
the first symbol in a bracket expression
(i.e., "%[^a-zA-Z]%" matches a string with a character that is not a
letter between two percent signs). But that would mean that I would have
to list each symbol I dont want and that would be undesireable as it
would be better to list exactly what the acceptable characters are.
Can anyone give me some insight as to where I'm going wrong?
thanks
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php