简体   繁体   中英

Building links in PHP

I'm trying to build and display a link in PHP with the code:

$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. Thus, 'shoes' works but 'our shoes' does not. With $fileName = 'shoes' I get the link:

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

which works fine.

With $fileName = 'our shoes' I get the link

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

I've tried putting the $feedName variable in single quotes, as below:

$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. Your browser would then move on at the space, and in the case of our shirts , shirts would become a new tag.

Just add quotes:

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

Example of the comparison: https://eval.in/574053

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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