3

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/abc?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?

5 Answers 5

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

I added an answer to show simple way to escape the encoded values of the URL.
1

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);

Comments

0
$url = 'http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT'.$i.'=Value';

Comments

0

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;
 }

Comments

0

I have got like this issue with using file_get_contents that get url query for 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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.