I'm using TinyMCE to save some HTML into an SQL table in phpMyAdmin. Inserting and retrieving the row from the table works fine.
I'm using a regex to translate some short codes in the retrieved text and this is where the problem arises.
This is my regular expression, which simply gets the text between two short codes with possible html tags and new lines:
/(<.+>)?[[]{$code}[]](<\/.+>)?((?:\n.+\n?)+)(<.+>)?[[]{$code}[]](<\/.+>)?/
When I retrieve the HTML from the DB and run the regex on it, the preg_match_all() fails to match anything, but when I double-click on the row in the database and open the in-line editor, phpMyAdmin does...something and performs an update on the row automatically and sets the text to a new value; Then, when I run the regex on the newly updated value, preg_match_all() matches the correct values.
I was thinking it was some automatic text encoding conversion or something, but running mb_detect_encoding() on the HTML before I insert it indeed confirms that the encoding is UTF-8 same as the table's utf8_unicode_ci.
I then compared the text plus EOL characters before and after the update in Notepad++ and they're exactly the same, yet my regex doesn't work before phpMyAdmin updates it.
What is phpMyAdmin doing to fix the text and how can I do it before it gets inserted in to the database? Why is it automatically updating the row at all?
I added some more code to the regular expression to check for content after the short code on the same line and now preg_match_all() matches correctly every time. I'm still not sure what's going on there as the content before and after the update are identical in every test I've tried (Same text, same amount of spaces and new line characters).
Regardless, I fixed it by adding the below regex after the check for the end HTML tag:
(?:.+)?
So the full expression is:
(<.+>)?[[]{$code}[]](<\/.+>)?(?:.+)?((?:\n.+\n?)+)(<.+>)?[[]{$code}[]](<\/.+>)?
Related
I have a problem related with linebreaks of an inported text, previously inserted in MySQL using the classic PHP - HTML form method.
The problem is the following. I want to load the text saved in MySQL databe into a "news" section in my web page. The thing is that my web page has a PC version and a mobil version, each one with different widths.
So, I don't want to insert linebreaks when I submit the text to MySQL (I mean the line-breaks the form will submit if the text line width excedes a hipotethical "cols" width, assuming I'd do a "physhical" or "hard" wrap; the manually inserted line-breaks to separate parragraphs I want to keep of course) because, as far as I know, you have to specify "cols", which is a parameter that will tell the width of the lines before doing the linebreaks.
This is not interesting, because the text fields on my "news" sections will have different widths, as I've told you, so importing text with linebreaks from MySQL won't adjust the the two different "news" sizes in my web.
What I need is to upload text to MySQL with no linebreaks, and then, let the two "news" sections in my web do the formatting of the text.
I though this would be easy, as I'm just parsing the saved text in MySQL databse into a <div> tag with a specified width. But the thing is that the text overflows the <div> container width every time.
This is what I'm using as the text input in the HTML form:
<textarea name="STORY" wrap="physical">EXAMPLE</textarea>
To inject the news in MySQL I use the typical PHP:
$var = "INSERT INTO exampleTable ('story') VALUE ($_POST['STORY']);
To load the saved text, I just echo the value of a variable that imports the text from the story field of MySQL database between div tags:
echo "<div>".$story."</div>";
As you can see, because I don't wan´t to use a "hard" wrap when I insert the text from the from in MySQL to avoid inserting line-breaks in the lines that otherwise would exced the "cols" width, I use a "phisycal" wrap, but I don't specify "cols", so I think that should prevent the form from inserting line-breaks other than the ones I do manually (presing "enter" key).
But the resulting text, when I echo it, will overflow my div width, as I've told you before.
Shouldn't the div width wrap the text inside of itself?
Should I delete the wrap="physhical" attribute from the form?
First, please note that you're insertion into the database is very likely to be insecure, and will probably result in SQL Injections.
To remove every linebreak, you can do things like that:
echo "<div>".str_replace("\n", "", $story)."</div>";
You could try handling this in MySQL itself using the TRIM() function:
INSERT INTO exampleTable ('story') VALUE TRIM(TRAILING '\n' FROM $_POST['STORY'])
You shouldn't insert line breaks yourself - CSS can do this for you. Here is a SO answer that appears to do everything you might want to do.
I am using a form to load text into a database. Then, I'm pulling the text back out with php in order to dynamically create my CSS selectors. Then I'm using javascript to toggle the selectors on and off as needed. Everything works well except the retrieved data has long lines of spaces in it (like the entire textarea of the form is included) and therefore, the javascript doesn't work due to the extra spaces. Because these are labels, they vary in length. I've tried storing the data as VARCHAR CHAR and TEXT.
How can I retrieve only the characters from MySQL? I have seen it somewhere in the manual but can't seem to find it now.
Thanks
You could just use trim in PHP. MySQL also has a TRIM().
Note that this removes all whitespace before and after the string, not space within the string.
You also will apparently always have a newline submitted because of the whitespace in the html itself. You should probably close the textarea without creating additional whitespace.
Do you just want to replace all white spaces?
REPLACE(col, ' ', '')
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace
HI,
I am creating on comments form where users will be commented and will be stored in the MYSQL database. The problem what I am facing is, it is stored as the single line in the database. It should be stored with exact format how user is entered in the form(like new lines and everything). I am using PHP to store it in the MySQL db.
First store it as text or longtext. Second, when showing the comment, use a function like nl2br to convert newlines to html <br> elements. This way, linebreaks are preserved.
Your text is stored just fine in the database if you are putting it into a long enough text-type field (e.g. TEXT), including the newlines in the user input.
Your problem is how to display the text formatted the way the user was seeing it when entering it. This is a more generic problem, and it only has to do with how HTML treats whitespace.
One approach would be to call nl2br on the comments, as Ikke says. This would replace all newlines (which the browser disregards) with <br> tags which have a visible effect on the rendered output.
Another option would be to put the text inside a <pre>...</pre> tag. This will force the browser to render it with whitespace preserved.
It's really up to what's more convenient/suitable for you.
Update: Just to be clear: do not modify the user input before inserting it in the database (unless it's part of your input validation, like e.g. stripping HTML tags from the input). Store it in an "untouched" format, and only do some processing on it before you output the data. This way, you always have the option of performing the correct processing if your output channel changes in the future (e.g. export comments to a text file vs displaying them as HTML).
you can store the comments in the same form in the mysql database. one difference would be when you retrieve the comments that has new line your code should look for \r\n and interpret it.. and also when you insert the data in mysql you will have to escape ' and \ characters from the comment.
I am having a real headache with reading in a tab delimited text file and inserting it into a MySQL Database.
The tab delimited text file was generated (I think) from a MS SQL Database, and I have written a simple script to read in the file and insert it into an existing table in my MySQL database.
However, there seems to be some problem with the data in the txt file. When my PHP script parses the file and I output the INSERT statements, the values in each of the fields are longer than they should be. For example, the first field should be a simple two character alphanumeric value. If I echo out the INSERT statements, using Firebug (in Firefox), between each of the characters is a question mark in a black diamond. If I var_dump the values, I get the following:
string(5) "A1"
Now, this clearly shows a two character string, but var_dump tells me it is five characters long!!
If I trim() the value, all I get is the first character (in this case "A").
How can I get at the other characters, even if it is only to remove them? Additionally, this appears to be forcing MySQL to insert the value as a BLOB, not as a varchar as it should.
Simon
UPDATE
If I do:
echo mb_detect_encoding($arr[0]);
I get a result of 'ASCII'. This isn't multibyte, is it??
Sounds like an encoding issue.
Are you running any strings through PHP functions which are not multi byte safe?
You may need to look at multi byte aware functions in PHP.
OK, solved all these issues by opening the TXT file in notepad and saving it specifically as UTF-8.
I still don't know what encoding was used (maybe UNICODE??) but it's all sorted now
Quick and simple (I hope) question; If a user inputs new lines in a text field, ie:
43 Dennis
Beeston
How can I save the new line rather than have it transferred to the mysql server as one line?!
I am using PHP and mysql.
When you output the field from the database to a html document, either use <pre> or nl2br() (or a <textarea>).
To save the newlines in the database, take a look at mysql_real_escape_string
If you want your newlines displayed as breaks on the page when you display the records, use nl2br.
MySQL should preserve the newline - could it be your output (or your mysql gui) that isn't displaying it?