Assume that I have a URL like this
http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT1=Value
In this URL, TEXT1 at the end keeps changing for various pages. The Value will not change though. So it will be something like
For Page 1
http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT1=Value
For Page 2
http://subdomain.domain.com/folder1/ab开发者_运维百科c?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT2=Value
For Page n
http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXTn=Value
How can I parametrize it? I tried something like this
for ($i=1;$i<=n;$i++)
{
$url = sprintf('http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT%d=Value',$i)
echo $url;
}
but it failed saying Sprintf too few arguments. Any suggestion, please?
You have more than one % sign in that url, sprintf parses it and tries to assign arguments to every %'something' it finds, you should escape the url encoded values.
You might want to check: http://www.php.net/manual/en/function.sprintf.php
Just use urldecode because the more than one (additional)%
is creating problem .
$url=urldecode('http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT%d=Value');
$url = sprintf($url,$i);
$url = 'http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT'.$i.'=Value';
You can use the normal string also right, instead of using sprintf
for ($i=1 ; $i < = n ; $i++ )
{
$url = "http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT".$i."=Value";
echo $url;
}
I have got like this issue with using file_get_contents
that get url query for solr search engine. I just solved it by escaping the %
percent sign in the url encoded string by adding an extra %
before every %
in the string as follows:
$str = "%s?q=WebSite:%s&sort=Date%%20desc&version=2.2&start=%s&rows=%s&indent=on&wt=json";
return sprintf($str, $this->url, $this->website, $this->start, $rows);
Notice the double % after the Date
in the string.
精彩评论