PHP regex sytax with preg_replace -
php regex weakness of mine, still manage things done online tools. consider following:
a subject string follows pattern: 1551 utc 04 june 2012
i want extract "04" , assign $day variable using below:
$day = preg_replace("/^([0-9]{4})\s([a-z]{3})\s([0-9]{2})\s([a-za-z]{3,})\s([0-9]{4})$/", "$3", $weather['date']); this works on following website: http://sqa.fyicenter.com/online_test_tools/test_regular_expression_search_replace.php
but can't work in script... $day equal whole subject string.
the result of var_dump() string(38) "1551 utc 04 june 2012 ". has 38 chars while should 21. looks there multiple whitespaces in string.
try trim() input string , replace \s \s+ support multiple whitespaces:
$day = preg_replace("/^([0-9]{4})\s+([a-z]{3})\s+([0-9]{2})\s+([a-za-z]{3,})\s+([0-9]{4})$/", "$3", trim($weather['date']));
Comments
Post a Comment