I want to test if an input is of the type 1234567 (7 digits). I use
preg_match($pattern,$subject);
where
$pattern = '/^[0-9]{7}$/';
However I get 1 for the case where the subject is
L-8987765
Which I do not want. How can I overcome this?
function isDocid($documento_id)
{
$subject = pg_escape_literal($documento_id);
$pattern1 = '/^[0-9]{7}$/';
if (preg_match($pattern1, $subject) === 1)
{
return 1;
}
else
{
return 0;
}}
$test_carta = isDocid('L-8987765');
echo('<p> ja existe docid '. $test_carta .'</p>');
I am expecting: ja existe docid 0
From the documentation (italics are mine):
pg_escape_literal()
escapes a literal for querying the PostgreSQL database. It returns an escaped literal in the PostgreSQL format.pg_escape_literal()
adds quotes before and after data.
In other words, you were never going to get a positive result from your regular expression check because of the quotes in the way. Don't use database escapes until you are ready to insert data into a database query. Or, better yet, don't insert a PHP variable into a database query – use prepared statements instead.
function isDocid($documento_id)
{
return (preg_match('/^[0-9]{7}$/', $documento_id) === 1) ? 1 : 0;
}
$test_carta = isDocid('L-8987765');
echo('<p> ja existe docid '. $test_carta .'</p>');