how to encode href attribute in HTML

后端 未结 1 1854
走了就别回头了
走了就别回头了 2020-12-17 10:57

What should be done against contents of href attribute: HTML or URL encoding?

link text

On the one hand, si

相关标签:
1条回答
  • 2020-12-17 11:22

    Construct a URL as normal. Follow the rules for constructing URLs. Encode data you put into it.

    Then construct HTML as normal. Follow the rules for constructing HTML. Encode data as you put it into it.

    i.e. Do both (but in the right order).

    They aren't mutually exclusive, so there is no contradiction.

    For example (this is a simplified example that assumes data in $_GET is correct and exists, don't do that in the real world):

    $search_term = $_GET['q'];
    $page = $_GET['page'];
    $next_page = $page + 1;
    $next_page_url = 'http://example.com/search?q=' . urlencode($search_term) . '&page=' . urlencode($page);
    $html = '<a href="' . htmlspecialchars($next_page_url) . '">link text</a>';
    
    0 讨论(0)
提交回复
热议问题