How to remove content from url after question mark. preg_match or preg_replace?

后端 未结 2 946
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-20 14:01

Im working on some function to grab few data from url with simple html dom.

But one of the data is an image and image have question mark and some more info behind it

2条回答
  •  再見小時候
    2021-01-20 15:00

    Even easier: use explode():

    list($uri,) = explode('?', 'http://somesite.com/uploaded/images/8.jpg?m=eSuQKgaaaa&mh=t0i7nVhjZleTJ5Ih');
    

    update

    Or simpler yet use strtok and trim:

    $uri = trim(strtok('http://somesite.com/uploaded/images/8.jpg?m=eSuQKgaaaa&mh=t0i7nVhjZleTJ5Ih', '?'));
    

    Normally you would use parse_url() for working with URLs but in a case like this, using explode() is simpler to use and serve's your purpose.

提交回复
热议问题