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

风流意气都作罢 提交于 2019-12-02 03:56:30

问题


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.

So example of url would be something like this.

http://somesite.com/uploaded/images/8.jpg?m=eSuQKgaaaa&mh=t0i7nVhjZleTJ5Ih

So this content behind question mark is some sort of the code that makes images resized to small size.

If i would just take this

http://somesite.com/uploaded/images/8.jpg

Images would be in bigger resolution and that's what i need.

I know there is a function like preg_match but i never understood expressions in it.

Is it possible that i somehow remove questionmark and all the content behind it ?


回答1:


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.




回答2:


In a much easier way it could be:

$iWantThisURL = substr($curr_url(), 0, stripos($curr_url, '?'));


来源:https://stackoverflow.com/questions/22702044/how-to-remove-content-from-url-after-question-mark-preg-match-or-preg-replace

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