What is a snippet url and why should I use it?

I am new to PHP Development.

Today I came across an interesting topic of URL fragmentation, specifically the "#" part of URLs.

I searched for what it says it looks like www.example.com \ foo.html # bar.

But I don't understand why this "#bar" is needed. and how to read it using PHP?

+5


source to share


3 answers


A snippet is an internal link to a page, sometimes referred to as a named anchor. It usually appears at the end of a URL and starts with a hash character (#) followed by an identifier. This refers to a section on a web page.

In HTML documents, the browser looks for an anchor tag with a name attribute that matches the snippet.



There are several things about snippets, the most important of which may be that they are not sent in HTTP request messages, but you can find more information about them on this page .

Javascript can manipulate snippets on the current page, which can be used to add history entries for the page without forcing a hard reload.

+13


source


Can't read it by php. It uses the client side (browser) for hash navigation, but you can write JS code to handle the hash change and send an asynchronous request to the server side (php) and display result on your page.



0


source


To read a fragment from PHP you can use the 'parse_url ($ url, PHP_URL_FRAGMENT)' function. This function is in a built-in PHP function. The following example will help you understand how to use it:

$url = 'www.example.com\foo.html#bar';
echo '<pre>';
var_dump(parse_url($url, PHP_URL_FRAGMENT));
echo '</pre>';

      

result:

string (3) "bar"

For more information on parse_url you can read this page

0


source







All Articles