Please forgive me if this question appears too cheap.
I have this line:
preg_match_all($pattern, $_GET['id'], $arr);
When you pass a value with space during a search, the value breaks once a space is encountered.
For instance:
36 2543541284
Notice a space between 6 and 2. In a situation similar to this, only 36 is displayed.
The reminder of digits after space are ignored. This is giving users, “no data found” message.
I have tried using urlencode to add 20% but no luck.
preg_match_all($pattern, rawurlencode($_GET[id]), $arr);
I have also tried urlencode but to no avail.
What am I possibly doing wrong?
function format($matches)
{
return $matches[1][0].(strlen($matches[2][0])>0?$matches[2][0]:" ").$matches[3][0].(strlen($matches[4][0])>0?" ".$matches[4][0]:"");
}
// CONSTRUCT A REGULAR EXPRESSION
$pattern
= '/' // regex delimiter
. '(' // START of a capture group
. '\d{2}' // exactly two digits
. ')' // END of capture group
. '(' // START SECOND capture group
. '[ND]?' // letters "D" OR "N" in any order or number - This is optional
. ')' // END SECOND capture group
. '(' // START THIRD capture group
. '\d*' // any number of digits
. ')' // END THIRD capture group
. '(' // START FOURTH capture group
. 'GG' // the letters "GG" EXACTLY
. '[\d]*' // any number of digits
. ')' // END THIRD capture group
. '?' // make the LAST capture group OPTIONAL
. '/' // regex delimiter
;
preg_match_all($pattern, rawurlencode($_GET[id]), $arr);
// REFORMAT the array
$str = format($arr);
// show what we did to the data
echo '<pre>' . PHP_EOL;
echo '1...5...10...15...20...' . PHP_EOL;
echo $pattern;
echo PHP_EOL;
//Are we getting what we asked for? This is a test. We will comment out all 6 lines if we are happy with output of our REFORMATTING.
echo $str;
echo PHP_EOL;
Your regex as it stands will not match two digits followed by a space and further digits.
If you want it to, you could change
[ND]?to[\sND]?, although this would also allow a space if the string wasn’t all digits.You need to specify the rules precisely if you want further advice on the regex.