Replace based on regex and return manipulated string

有些话、适合烂在心里 提交于 2019-12-12 02:48:39

问题


I have a logic query as a string, for example:

mydate == 'one day ago' && mydate > 2014-05-16 16:00:00

I need every instance of a date replaced by a timestamp. My current code can replace the YYYY-MM-DD HH:MM:SS date-time to a timestamp using strtotime():

$my_string = "mydate == 'one day ago' && mydate > 2014-05-16 16:00:00";

// Pattern: *YYYY-MM-DD HH:MM:SS*
$pattern = '((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))';

$my_string = preg_replace_callback($pattern, function($m){return strtotime($m[0]);}, $my_string);

echo($my_string);

Output:

mydate == 'one day ago' && mydate > 1400252400

However, I also want to support natural language relative phrases. The query string is always of a set structure:

mydate OPERATOR 'DATE' && mydate OPERATOR 'DATE'...

What I'd like to do is find the variable mydate skip past the operator then get the contents within the single quotes, apply strtotime() to the contents of the single quotes and then replace the date (including single quotes) with the integer. For example:

Input:

mydate == 'one day ago' && mydate > '2014-05-16 16:00:00'

Target process:

mydate == timetostr('one day ago') && mydate > timetostr('2014-05-16 16:00:00')

Output:

mydate == 123123123123 && mydate > 1400252400

Note the difference between my current and the new input, now I want both instances to have single quotes and not use a regex designed only for date-time strings.

How can I make a regex along the lines of variable_name*'*'

Where * is any character, take the contents of '' and return variable_name*


回答1:


Try using: [^\-+\*=]+\s*[\-+\*=]{1,2}\s*('[^']+')\s*\&\& (regex101)

The first character class is any non-operator character 1 or more times, then 0 or more whitespace characters, then 1 or 2 operator characters (you may or may not want to include angle brackets here) then 0 or more whitespace characters then you capture the '*' as you've termed it above.




回答2:


I have settled with this based on information on @adamdc78's link

$str = "mydate == '1 day ago' && mydate != '2014-05-16 16:00:00' && myname = 'Craig' || mydate > '2014-05-16 16:00:00'"; 

$var_name = 'mydate';

$re = '/'.$var_name.'\s*[\-+\=\<\>\!]{1,2}\s*(\'[^\']+\')/'; 

$my_string =    preg_replace_callback(
                    $re,
                    function($m)
                    {
                        foreach($m as $key=>$value)
                        {
                            $m[0] = str_replace('\'','',$m[0]);
                            $m[1] = str_replace('\'','',$m[1]);
                            $m[0] =     str_replace($m[1],strtotime($m[1]),$m[0]);
                        }

                        return($m[0]);
                    },
                $str
                );

echo($my_string);

Output

mydate == 1400245345 && mydate != 1400252400 && myname = 'Craig' || mydate > 1400252400



来源:https://stackoverflow.com/questions/23701180/replace-based-on-regex-and-return-manipulated-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!