I am struggling to do something which appears quite simple...
I use PHP cURL to scrape data and insert it into my website. cURL saves the data as a string in $data
before it is output.
What I am trying to do is target all of the URL's contained within $data
. The URL's sometimes contain a fixed value parameter that I need move to the end of the URL. The URL's look like this, where category=widgets
can appear anywhere in the URL:
http://www.mysite.com/script.php?category=widgets&show=all&size=big
I need to move the parameter开发者_运维知识库 category=widgets
to the end of all URL's, so they look like this:
http://www.mysite.com/script.php?show=all&size=big&category=widgets
I'm thinking that I can firstly remove all occurences of category=widgets
with str_replace, that's the easy bit.
The problem I have is appending category=widgets
to the end of the URL. Because the URL is dynamic, perhaps preg_replace is more appropriate. I'm new to regular expressions, and it's giving me a headache.
Would appreciate your help. Thanks.
I'd recommend making use of the parse_url, as this is liable to be considerably more robust in the long term than string manipulation.
As such, you could use parse_url to extract the various chunks and then assemble a new URL based on these as required.
精彩评论