문제

I want to take var1 and var2 out of the query string and write it (echo) into the HTML.

Seems simple, but my URL needs to be formed like this:

http://example.com/filename.html#var1=FOO&var2=BAR

Notice the hash after the filename -- URL does not contain normal ? query string delimiter.

How do I accomplish this with a simple function? ... seems like a basic question, but I can't get it to work with URL syntax that requires # before ?.

thanks in advance!

UPDATE UPDATE UDATE - building on the code example in @Musa's answer.

I want to grab the PHP_URL_FRAGMENTS from the window location, and write them into the HTML, I am attempting this:

// get the URL from window location:
$pageURL = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
$pageURL .= $_SERVER['SERVER_PORT'] != '80' ? $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"] : $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
return $pageURL;

// parse URL including #fragments:
parse_str (parse_url ($pageURL, PHP_URL_FRAGMENT ), $args);

// write var values into the HTML:
    echo $args['var1'], ' ', $args['var2'];

I'm either not creating $pageURL properly, or I'm doing something wrong in the parse_str line.

Still working with an URL syntax that looks like this: http://example.com/filename.html#var1=FOO&var2=BAR

Any solutions from you wizards? Thank you.

도움이 되었습니까?

해결책

parse_str (parse_url ("http://example.com/filename.html#var1=FOO&var2=BAR", PHP_URL_FRAGMENT ), $args);
echo $args['var1'], $args['var2'];

http://php.net/manual/en/function.parse-url.php

http://php.net/manual/en/function.parse-str.php

DEMO

다른 팁

You can pass it through Javascript or send it to server as a hidden form element.

The part after sharp symbol (hash part) usually will not be passed to the server by browser.

You can check your server logs - received get requests will be "http://example.com/filename.html", but not "http://example.com/filename.html#?var1=FOO&var2=BAR"

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top