PHP to check if a URL contains a query string

前端 未结 4 476
闹比i
闹比i 2020-12-05 20:11

This is an easy one. There seem to be plenty of solutions to determine if a URL contains a specific key or value, but strangely I can\'t find a solution for determining if U

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 20:47

    parse_url seems like the logical choice in most cases. However I can't think of a case where '?' in a URL would not denote the start of a query string so for a (very minor) performance increase you could go with

    return strpos($url, '?') !== false;

    Over 1,000,000 iterations the average time for strpos was about 1.6 seconds vs 1.8 for parse_url. That being said, unless your application is checking millions of URLs for query strings I'd go for parse_url.

提交回复
热议问题