Load string into html from file? Preferably not using javascript - php

I have the following code in index.html:
<div class="button">
Title
</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">
Title
</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&trail=0&grid=1&colors=1&zoom=1&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!

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.
Title
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.

Related

Php isset not running

Hi guys may I ask why is my isset failed to ready parameter from the url?
The second picture is I tried to pass a id parameter into it
You didn't explain precisely what the code is doing instead of what you expected, but from the code we can see some likely issues:
Your URL looks wrong:
A # in a URL normally tells the browser to move to an anchor in the current page, not send a request to the server
Unless you've got some system for enabling "pretty" URLs then you'll need to put the .php extension on the end of the filename in the URL
%s isn't a number or ID, it looks like it might be a placeholder for string replacement, but it's unclear from the code you've shown whether the text is actually inside some sort of string / command where replacement would occur.
A valid-looking example (relative) URL to put into your link might be something like delAcc.php?id=1, so
<a href="delAcc.php?id=1" class="fas fas-user-minus">
in PHP, variables inside single-quoted strings are not interpolated, so you'd get $id literally shown on screen in the alert.
You need to double-quote the string:
echo "<script>alert($id)</script>";

Quotation marks magically alter file path in PHP

Inside "the loop" echo get_permalink() or the_permalink() work fine and produce something like http://www.example.com/path-to-post, until I put these calls inside of quotes in the HTML like so.
<p>
This is normal HTML!
"<?php the_permalink()?>"
</p>
At which point it magically becomes a site relative url, like just "/path-to-post".
I just figured out that I can avoid this problem by putting a space between the quote mark and the php, which works fine for links and stuff, but what's going on here? Since when is PHP able to read the content outside of php blocks and react to it? And why would this happen anyway?
Q: Since when is PHP able to read the content outside of php blocks and react to it?
A: For a very long time (think PHP 4) it's been able to capture the outputted text into a buffer and then read that buffer like a string. See ob_start(). Technically a function could just check if the last character on the buffer is a " and behave differently in that situation.
Q: Why would this happen anyway?
A: Are you sure this is happening with the the_permalink() function? There could be a plugin which is attempting to make sure that absolute urls don't get used in <a href="<?php the_permalink() ?>"> context. Which plugins do you have installed? Maybe that's even default behaviour and I'm just not seeing it on my quick scan of the_permalink() in wp-includes/link-template.php.
You could try a few other ways to get around it. The first thing I would try is using " instead of a literal ".
Well, I'm a fool. There was a plugin, called "Absolute Relative Links" no less, which was reformatting the page. Still, strange the way it operates. It seems to look for a quotation mark immediately followed by anything resembling a URL and reformats it. But even a single space is enough to stop it. And I didn't realize PHP could read stuff on the page outside of itself. Lesson learned.

What would cause html code formatting to disappear in php page?

I have a page written with php where, for some reason, all of the plain html content of the file index.php goes on one line (look at the source) The white space is preserved, but all the new-lines disappear.
I cannot come up with any reason why this would happen, short of a syntax error, but I went through with a fine toothed comb, and found nothing out of place. This only happens on the index.php page.
Anyone have any Ideas what I should be looking for? I can post more code if necessary.
<?php
//...
include('ssi/header.php');
?>
<div>
<section id="charters">
<h2>Tanker Chartering</h2>
<!-- ... -->
The above code evaluates to something like this:
<div> <section id="charters"> <h2><a href="charters.php">Tanker ...
Maybe you have linux server and you're using windows system. Different operating systems use different new line characters. Also, for one server my FTP client uploaded it with wrong formatting, and missed every line break.
Also applications like
Notepad++ gives you the ability to change formatting and linebreaks.
It's probably the encoding of the file combined with the transfer mode on the ftp from which you downloaded/uploaded the file. Try using something like notepad2, and saving the file in UTF-8 rather than ANSI. Also upload/download with your FTP program in binary not ASCII. That stopped all of my newline issues with PHP.
could it be that your hosting provider is doing some kind of minimization for you? no newlines means less characters pused down the wire.

Using PHP to search a text file

I'm trying to create a code to download mp3 files embedded in a page. It starts out as a submit form. You input the URL and submit it, and it writes the HTML source of that page to a text file. I also set the script to search the source to see if there is an audio file embedded. I suppose I should include that it's not in the format of filename.mp3. The format is:
embed type="application/x-shockwave-flash" src="http://diaryofthedead.tumblr.com/swf/audio_player_black.swf?audio_file=http://www.tumblr.com/audio_file/1435664895/tumblr_lb2ybulZkt1qb5hrc&color=FFFFFF" height="27" width="207" quality="best"
So here's the thing, there's just a certain string you have to add to the end of the file, for it to redirect to the mp3 file. I know the string. What I want to do is extract, for example "http://www.tumblr.com/audio_file/1435664895/tumblr_lb3ybulZkt1q5hrc" from the middle of this. I know how to read from files but I have no idea how to extract certain parts from it without knowing the exact filename already. So is there any way I can have it search the source for "audio_file" and if it finds the string, extract the audio file?
If your program is just a parser for extracting MP3 files embedded in a webpage you don't even need to save the contents of the webpage onto a file, you can work with the page source inside just your server's memory.
If you want to detect paths to MP3 inside flashes, provided you know how does it match a regular expression, you are done.
If you don't know much about rgular expressions, you should look at them.
If you don't want as much power as a regular expression can give to you, you can always find strings by position, like:
$pos = strpos($haystack, $needle);
Beware: strpos() will find the first (strrpos will find the last) occurrence of a string. So you need to make it as explicit as you can, or you might end up capturing something unwanted.
Take a look at http://www.regular-expressions.info/quickstart.html or something similar.
I can't post more links because I don't have enough reputation yet
You can try using preg_match (http://php.net/manual/en/function.preg-match.php) to get the contents between "audio_file=" and "&".
Or you can also use a string between function to get the contents between those two strings:
http://www.php.net/manual/en/function.substr.php#89493

Why mysql is not storing data after "#" character?

I have made one form in which there is rich text editor. and i m trying to store the data to database.
now i have mainly two problem..
1) As soon as the string which contents "#"(basically when i try to change the color of the font) character, then it does not store characters after "#". and it also not store "#" character also.
2) although i had tried....in javascript
html.replace("\"","'");
but it does not replace the double quotes to single quotes.
We'll need to see some code. My feeling is you're missing some essential escaping step somewhere. In particular:
As soon as the string which contents "#"(basically when i try to change the color of the font) character
Implies to me that you might be sticking strings together into a URL like this:
var url= '/something.php?content='+html;
Naturally if the html contains a # symbol, you've got problems, because in:
http://www.example.com/something.php?content=<div style="color:#123456">
the # begins a fragment identifier called #123456">, like when you put #section on the end of a URL to go to the anchor called section in the HTML file. Fragment identifiers are purely client-side and are not sent to the server, which would see:
http://www.example.com/something.php?content=<div style="color:
However this is far from the only problem with the above. Space, < and = are simly invalid in URLs, and other characters like & will also mess up parameter parsing. To encode an arbitrary string into a query parameter you must use encodeURIComponent:
var url= '/something.php?content='+encodeURIComponent(html);
which will replace # with %35 and similarly for the other out-of-band characters.
However if this is indeed what you're doing, you should in any case you should not be storing anything to the database in response to a GET request, nor relying on a GET to pass potentially-large content. Use a POST request instead.
It seems that you are doing something very strange with your database code. Can you show the actual code you use for storing the string to database?
# - character is a common way to create a comment. That is everything starting from # to end of line is discarded. However if your code to store to database is correct, that should not matter.
Javascript is not the correct place to handle quote character conversions. The right place for that is on server side.
As you have requested....
I try to replay you... I try to mention exact what I had done...
1) on the client side on the html form page I had written like this..
html = html.trim(); // in html, the data of the rich text editor will come.
document.RTEDemo.action = "submit.php?method='"+ html.replace("\"","'") + "'";
\\ i had done replace bcz i think that was some problem with double quotes.
now on submit.php , my browser url is like this...
http://localhost/nc/submit.php?method='This is very simple recipe.<br><strong style='background-color: #111111; color: #80ff00; font-size: 20px;">To make Bread Buttor you will need</strong><br><br><blockquote><ol><li>bread</li><li>buttor</li></ol></li></blockquote><span style="background-color: #00ff80;">GOOD.</span><br><br><br><blockquote><br></blockquote><br>'
2) on submit.php ........I just write simply this
echo "METHOD : ".$_GET['method'] . "<br><br>";
$method = $_GET['method'];
now my answer of upper part is like this...
METHOD : 'This is very simple recipe.
now i want to store the full detail of URL....but its only storing...
This is very simple recipe.

Categories