Isabell Isabell - 4 months ago 9
PHP Question

find word/string in string , do I need regex?

I got following String:

... 12:32 +0304] "GET /test.php?param=value ....


I want to extract the test.php out of this String. I tried to find a php function wich could do this but there was nothing helpful. So my next guess was, what about regex and I tried for so long to get the part between GET / and ?. I failed badly... Does a function in php exists wich could help me out or do I need regex for this? If I do so, how can I get a string out of a string? Important, I don't want to know if test.php is in the string. I want to get everything between GET / and ?.

Thanks a lot

Answer

The regex extracting anything between GET / and ? in a capture group:

GET \/(.*?)\?

Demo: https://regex101.com/r/wR9yM5/1

In PHP it can be used like this:

$str = '... 12:32 +0304] "GET /test.php?param=value ....';
preg_match('/GET \/(.*?)\?/', $str, $re);
print_r($re[1]);

Demo: https://ideone.com/0XzZwo