PHP Regex won't match -
PHP Regex won't match -
i'm new php development (i'm learning myself many think) , regex, have next function among others in php file:
function func($data) { $posdata = preg_replace( '/([^.0-9]+)/', '', $data); // should remove periods , numbers. if ( preg_match( '/^([01][0-9]|20|[01][0-9][.][0-9]{1,2}|0[.][0-9]{1,2})\z/' , $posdata ) ) { homecoming $posdata; } else { homecoming false; } }
which supposed match eg: 20 | 0.25 | 12 | 08.54 | 15.6 , should not match 0, 7.12, higher 20 or period @ end. if check on regex101.com works fine, if run on site same page no function , pre-established info works fine. when send info trough form , utilize php file this:
<?php include_once'/path/to/functions.php'; $text = $_post['text']; if ( $a = func($text) !== false ) { $b = func($text); echo $b; } else { echo "failed"; } ?>
it matches 20 | 01 | 12 , 99 (which should not be) not match eg: 0.58 | 12.45 | 08.65 , , can't work. don't understand why happening.
pd: i'm not native english language speaker, i'm sorry misspelling or language syntax mistake. if help me create regex bit faster or shorter appreciated much.
well not know why, solved problem echoing javascript alert message containing text variable every time create step on function. uploaded functions page server, closed browser, cleaned browser data, open 1 time again , tried run form check 1 bad info eg: 99, alerted through process. tried 1 info eg: 08.20 , did not alerted message , function output false. figured out wrong page cache or (i wasn't sure it) decided include html tags:
<!doctype html> <html> <head> <meta http-equiv="cache-control" content="no-cache"> </head> <body> <?php // include functions.php // post values. // post info checking code // echo checking result ?> </body> </html>
and problem solved. if can explain me why happened, helpful.
php regex
Comments
Post a Comment