0

I have the following code in index.html:

<div class="button">
    <a href="ridiculously long string" title="title">Title</a>
</div>

I'd like to save "ridiculously long string" in a text file, referenced by index.html. Is this possible?

I tried replacing the string like so the following, but it doesn't work: php reference: file_get_contents()

<div class="button">
    <a href=<?php echo file_get_contents("text.txt"); ?> title="title">Title</a>
</div>

Errors symptoms: the button on my page now reads title="title">Title and clicking it takes me to a 404: The requested URL /~user/html_root/< was not found on this server.. index.html and text.txt are in the html_root directory.

Here's how one of the shorter text.txts read:

?autoplay=0&amp;trail=0&amp;grid=1&amp;colors=1&amp;zoom=1&amp;s=%5B{%228%22:%5B60,61,98,103,109,115%5D},{%229%22:%5B60,61,77,78,97,99,102,104,108,110,114,116%5D},{%2210%22:%5B76,79,98,103,105,109,111,115,117%5D},{%2211%22:%5B76,79,104,110,112,116,118%5D},{%2212%22:%5B60,61,63,64,77,78,111,117%5D},{%2213%22:%5B60,61,63,64%5D},{%2219%22:%5B76,77,79,97,98,102,103,108,109,114,115%5D},{%2220%22:%5B76,78,79,97,99,102,104,108,110,114,116%5D},{%2221%22:%5B98,103,105,109,111,115,117%5D},{%2222%22:%5B104,110,112,116,118%5D},{%2223%22:%5B61,111,117%5D},{%2224%22:%5B60,62,76,77%5D},{%2225%22:%5B60,62,75,78%5D},{%2226%22:%5B61,76,79%5D},{%2227%22:%5B77,78,96,97,102,103,109,110,115,116%5D},{%2228%22:%5B96,98,102,104,109,111,115,117%5D},{%2229%22:%5B61,65,97,98,103,105,110,112,116,118%5D},{%2230%22:%5B60,62,64,66,104,105,111,113,117,119%5D},{%2231%22:%5B60,62,64,66,75,76,112,113,118,120%5D},{%2232%22:%5B61,65,75,78,119,120%5D},{%2233%22:%5B77,78%5D},{%2237%22:%5B78,79%5D},{%2238%22:%5B77,79%5D},{%2239%22:%5B77%5D},{%2240%22:%5B60,61,63,64,75,77%5D},{%2241%22:%5B61,63,75,76%5D},{%2242%22:%5B61,63%5D},{%2243%22:%5B60,61,63,64,114%5D},{%2244%22:%5B78,79,84,85,92,93,95,113,115%5D},{%2245%22:%5B79,84,86,92,93,95,96,97,104,112,115%5D},{%2246%22:%5B78,86,98,103,105,111,113,114%5D},{%2247%22:%5B75,77,86,87,92,93,95,96,97,102,105,110,112%5D},{%2248%22:%5B75,76,93,95,103,104,109,112%5D},{%2249%22:%5B93,95,110,111%5D},{%2250%22:%5B94%5D}%5D

I thought changing text.txt to a more benign URL might help debugging. I changed text.txt to https://www.google.com/ and get the same 404.

I could implement a javascript solution. There's already js on this webpage. But it's controlled by a colleague and I'd prefer to try a stand alone solution first. Many thanks to anyone who can help!

10
  • 3
    It might help us if you could explain why "ridiculously long string" was being used as the value of an href. Commented Nov 30, 2019 at 19:58
  • 1
    How does file_get_contents() fail? Commented Nov 30, 2019 at 19:59
  • 1
    @JohnHaTrick I don't understand, the href attribute gets completely removed? Commented Nov 30, 2019 at 20:16
  • 1
    @JohnHaTrick Why not explain further what it is exactly that you want to do and why? Commented Nov 30, 2019 at 20:22
  • 1
    @JohnHaTrick That's really not helpful to us... what's in the text file? Moving all this to external text files really sounds much less easier to read. If you could actually show us what's in the file, we can probably suggest a way to format your code. (For example, the HTML tag doesn't need to be all on one line.) Commented Nov 30, 2019 at 20:46

1 Answer 1

2

Anytime you want to inject arbitrary data into HTML, you need to wrap it with htmlspecialchars() so that any reserved characters are escaped. Additionally, you actually need to surround attribute values with quotes or you're going to be generating invalid HTML.

<a href="<?php echo htmlspecialchars(file_get_contents('text.txt')); ?>" title="title">Title</a>

Really though, "ridiculously long string" is questionable anyway. I assume you're using some huge data URI? If so, consider not doing that, as there are limits you'll run into and it's not efficient to base64-encode things.

Sign up to request clarification or add additional context in comments.

10 Comments

Ok one of my symptoms is now abated. The button now bears its correct label. The 404 error remains.
To be more specific, the 404 reads: "/~user/html_root/< was not found". html_root is the directory of my index.html and text.txt. I would expect something more like "text.txt was not found"...
@JohnHaTrick Surely that path is what's in your text file?
No path in the text file. It reads: ?autoplay=0&amp;trail=0&amp;other_variables=0&amp;...;s=%5B{%228%22:%5B60,61,98,103,109,115%5D},{ tons },{ and },{ tons },{ of },{ data }%5B
@JohnHaTrick Use backticks to format code on Stack Overflow, or your HTML can get eaten and it isn't clear what is in what. And you say tons-and-tons-of-data... can you show literally what's in it? Can you edit your question to show literally what's in that file, and what the actual HTML output is?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.