使用Wordpress保留URL中的Echo变量格式

时间:2014-11-01 23:26:05

标签: php wordpress

我在wordpress中为模板添加一个函数,用于将变量($ location)链接到搜索该位置的所有事件。这主要涉及使用$ location作为搜索字符串创建指向网站搜索结果的链接。然而,我遇到的问题是虽然在URL之外我可以调用$ location变量并且它保留位置名称中的空格:

NRS Arena

当我在url中使用变量时,它出现为

NRSArena

我的搜索字词。我真的需要将术语格式化为

NRS +竞技场 要么 NRS%20Arena

在搜索字符串中有效使用。

这是我的代码:

$html .= html( 'a', array( 'href' => esc_url( get_site_url() . '/?st=event&ls=' . $location . '&location='), 'target' => '_blank' ), $location));

所以在哪里

$ location = NRS Arena

我希望它像这样输出HTML:

<a target="_blank" href="http://www.mysiteurl.com/?st=event&ls=NRS+Arena&location=">NRS Arena</a>

甚至是NRS和Arena之间的%20都可以,而不是+

而是输出:

<a target="_blank" href="http://www.mysiteurl.com/?st=event&ls=NRSArena&location=">NRS Arena</a>

通过删除URL中单词之间的空格,显然网站搜索无法正常工作。我不明白为什么在URL之外使用$ location会保留空间,但在URL内部却没有。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您有两种选择:

  1. 使用rawurlencode - 返回%20
  2.   

    $ html。= html(&#39; a&#39;,数组(&#39; href&#39; =&gt; esc_url(get_site_url()。&#39; /?st = event&amp; ls =& #39; .rawurlencode($ location)。&#39;&amp; location =&#39;),&#39; target&#39; =&gt;&#39; _blank&#39;),$ location));

    1. 使用url_encode - 返回+
    2.   

      $ html。= html(&#39; a&#39;,数组(&#39; href&#39; =&gt; esc_url(get_site_url()。&#39; /?st = event&amp; ls =& #39; .url_encode($ location)。&#39;&amp; location =&#39;),&#39; target&#39; =&gt;&#39; _blank&#39;),$ location));

相关问题