简体   繁体   English

用PHP构建链接

[英]Building links in PHP

I'm trying to build and display a link in PHP with the code: 我正在尝试使用以下代码在PHP中构建和显示链接:

$displayLink = "<a href={$feed_url}{$feedName} download>$displayFeed</a>";
echo "<br />$displayLink";

This works fine, and I'm able to click the link in the HTML and follow it - unless $feedName contains a space. 这可以正常工作,并且我可以单击HTML中的链接并遵循它-除非$ feedName包含空格。 Thus, 'shoes' works but 'our shoes' does not. 因此,“鞋子”有效,而“我们的鞋子”无效。 With $fileName = 'shoes' I get the link: 使用$ fileName ='shoes',我得到了链接:

<a href="http://my_site/users/55/shirts.txt" download="">shoes</a>

which works fine. 效果很好。

With $fileName = 'our shoes' I get the link 使用$ fileName ='our shoes'我得到了链接

<a href="http://my_site/users/55/shirts.txt" download="">our</a>

I've tried putting the $feedName variable in single quotes, as below: 我尝试将$ feedName变量放在单引号中,如下所示:

$displayLink = "<a href={$feed_url}{'$feedName'} download>$displayFeed</a>";
    echo "<br />$displayLink";

but that gives me the link: 但这给了我链接:

href="http://my_site/users/55{'/our" shoes.txt'}="" download="">our shoes</a>

How can I get "our shoes" into the link? 如何获得“我们的鞋子”的链接?

Thanks 谢谢

I think this is actually just a problem with how you're constructing the anchor tag. 我认为这实际上只是如何构建锚标记的问题。 Because you don't add quotes around the href value, a space would signify a break in the tag's value. 因为您没有在href值周围添加引号,所以空格表示标记值的中断。 Your browser would then move on at the space, and in the case of our shirts , shirts would become a new tag. 然后,您的浏览器将在该空间继续前进,对于our shirts ,衬衫将成为新标签。

Just add quotes: 只需添加引号:

$displayLink = "<a href='{$feed_url}{$feedName}' download>$displayFeed</a>"; 

Example of the comparison: https://eval.in/574053 比较示例: https : //eval.in/574053

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM