This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: Truncate HTML, ignoring tags
I have html data saved in db. I want to display html as shortened..
I try to use mb_strstr function like this;
$str = mb_strstr($this->htmlData, "</p>",true);
echo $str."</p>";
It echos the first paragraph of the html. But the problem is html is filled in admin panel and sometimes first paragraph is not have enough text.
I also dont want to use fixed character position with substr because sometimes let say 200 character can be a html tag so it produces invalid format html formatted output.
So I want to learn best practice for this kind of problem.
Thank you.
Add some custom tag or code into your WYSIWYG editor (example: <separator> or ...). You could use it to separate introduction part from the rest of the article. That will help you avoid mess with PHP tags being unclosed in introduction part. Also it gives author option to decide manually which part of text would be good for introduction.
Another wise think that can be done would be to make a separate field in the database for introduction part. Yes, it would cost more memory but it would give an option to author to write juicy introduction text to have more people open full article...
Related
On Stackoverflow I've found questions about storing BBCode OR HTML into the database, but what about storing both? For example, I would create posts DB table with two columns: body_bbcode & body_html.
In body_bbcode I would store original post submitted by a user (forum member), and in body_html I would store parsed (HTML) version of that post.
So, for displaying forum posts I would use body_html, but for editing & quoting (replying with quote) I would use body_bbcode.
The reason why I want to do this is because the parser is using regex and without body_html it would need to convert at least 15 forum posts per topic page. Correct me if I'm wrong, but that can cause performance issues?
On the other hand, I didn't see anyone doing like this so I'm wondering what are the disadvantages of this approach, besides taking up more space in the Database?
Also, I am thinking of adding a new column in which I would store plain text version for search purposes, so that the tags themselves aren't searched (for example body_text).
The reason why I want to do this is because the parser is using regex and without body_html it would need to convert at least 15 forum posts per topic page. Correct me if I'm wrong, but that can cause performance issues?
A well designed bbcode regex will not hinder performance in any meaningful way.
Do not create "duplicate" columns for bbcode text and html text.
A major problem you run into with your suggested approach is that you will inevitably change your html code. (E.g., add a class to html links, change iframe dimensions of youtube embeds, etc.) Then you're stuck trying to update the data in the html column which would be problematic.
I am working on a simple php-MySql website and presenting the data for the following fields for each entry in the database (through a loop):
Title
Organisation
DetailedInfo
The 'DetailedInfo' field in the database can hold up to 5000 characters. While displaying on the webpage I am only using the first 250 characters.
The problem is as follows. If an entry has a formatting tag (italic/bold) starting, say at character 240, and the formatting tag is not closed by the 250th character then the problem starts. For all subsequent entries the Title, Organisation and DetailedInfo are displayed with the tag (so all the subsequent text are either italic, or, bold).
I am using CSS style for Title, Organisation and DetailedInfo but it seems that the CSS is not able to get rid of the formatting tag from the data.
Any help will be appreciated.
Cheers,
Tim
If you're only displaying a small portion of the detailedInfo field I'd guess formatting it isn't that mportant. Use strip_tags() to get rid of the formatting tags before you display it.
CSS cannot fix broken HTML. You'll need to strip it back to plain text and re-code (or just leave it out).
I wouldn’t fix that with CSS (and I don’t think it’s possible). You’re outputting invalid HTML, which is going to cause problems, especially if anyone ever looks a the page in IE 8 or earlier.
It could also be worse than an unclosed tag. What if the excerpt ends with </i?
I’d either implement some crazy logic to close any unclosed HTML tags in the 250-character excerpt, or strip all HTML tags from the excerpt. I’m guessing the latter would be easier.
This question already has answers here:
Rendering HTML inside textarea
(7 answers)
Closed 7 years ago.
I have code that lets you format the text in a textarea with bold, underline, strikeout or italics. But it displays the html tags instead of actually formatting it. I know once I output it inside a div, it ill be formatted, but I want to show the formatting while the user writes the text.
How can I enable formatting? JavaScript is fine, I'm using JQuery at the moment. I would also rather not use a WYSIWYG editor, if there is another option.
Thanks for any help.
Use contentEditable attribute on div element
<div contentEditable="true">
</div
This way you can format your input in any desired way, just dont forget properly escape data.
Just a thought; if you just want to let users see what their response will look like, you could take a cue from this site and have a sort of "preview" div right above/below the comment box. You could use onKeyUp to put the code into the div to keep it up do date as the user types. It's not perfect, but it's probably as good as you're going to get if you don't want to use a WYSIWYG.
That being said, the contentEditable + WYSIWYG option is probably more user friendly, and simpler, IMHO.
I believe it's only possible by using some div or other styleable html element instead of your textarea. With Javascript you would then add the functionality of the textarea.
How about you use some formatting system like this here on SO? It is much more elegant in my opinion, than pseudo WYSIWYG.
Replace the textarea with an div and enable the designmode.
Or you can use the jquery plugin http://plugins.jquery.com/project/designMode
This question already exists:
Closed 11 years ago.
Possible Duplicate:
How to parse HTML with PHP?
I would be most grateful if a regex master among you would be kind enough to help me.
I'd like to make a php function that converts html tags/elements, as per the following:
I want to convert
<span class="heading1">Any generic text, or other html elements such as <p> tags</p> in here</span>
To
<h1 class="heading1">Any text, or other html elements such as <p> tags</p> in here</h1>
...So basically I want to convert the span headings to proper h1 tags (this is for the purpose of better SEO) but there could be other normal span tags that I want to preserve.
Any ideas? Thanks in advance.
Well, as the commenters above pointed out, it's probably not a good idea. However, since this case is extremely simple, the regex would be pretty easy if you want to live on the edge:
preg_replace('/<(\/*)span/', '<${1}h1', $htmlFile);
This will replace all span tags with h1 tags. Note that if there is any deviation from the format, it will break. Hence the warnings against this method. I would only recommend it if you are working with a small number of relatively small HTML files, so you can check them for errors.
EDIT: Yeah, if you only want to replace ones with class="heading1" I'm not touching it. That would require more mucking about with the regex than it would probably take to just fix all the files manually.
EDIT 2: Okay, I'm a little bored and curious, so I'm going to see if I can come up with a regex that would replace all class="heading1" spans and their corresponding closing tags with h1's:
preg_replace('/<span class="heading1">(.*(.*<span.*>.*<\/span>.*)*.*)<\/span>/', '<h1 class="heading1">${1}</h1>', $htmlFile);
If my calculations are correct, this should ignore any matching sets of span tags inside the heading1 span tags.
You're still probably better off using a DOM parser though.
Hey guys Im building a web-app where users can login and post/read articles and comment and things.
Im giving them a form to post an article where they provide its title, description and text.
leaving the validations and sql injections aside (already done that), I need help with displaying the article stored in MySQL database as TEXT.
Im taking the article text from a textarea, and displaying it in a p tag but then obviously it skips the new line characters entered by the users, but the pre tag makes it ugly by giving a wide scrollable display.I want to know which tag is appropriate to be used for this purpose? or is even taking an article through textarea correct?
Im a learner and am building such a webapp with articles and comments sections for the first time, so any suggestions are most welcome. Thank you in advanced.
My recommendation would be of two choices:
1. Use Plain Text:
If you want that user can not put any HTML in the contents, show a simple HTML Textarea input to user, then when the user enters a new line (Enter key) it would be \n in your database. When you want to print the article just use nl2br($article_contents); and it will convert the new lines (\n) into HTML line breaks.
2. Rich Text:
If you want users to put HTML contents in article then it would be easy if you use any Text Editors like TinyMCE. TinyMCE will make it easy for your users to do simple HTML Formatting like headings, bold, italic, paragraph alignments, color, add images. Then in the PHP side use strip_tags function to allow only the certain tags so the user could not insert any malicious code like XSS injections into HTML contents. For example:
strip_tags($article_contents, "<u><b><i><font><span><p>");
Proposed Answer:
Use <span></span>
Tags like <p></p><div></div> take up as much space as they can, while <span></span> takes up as little as it can to hold whatever is inside it, so it might be more suitable for you.
Let me know if that worked for you.
In PHP you can use function nl2br that changes all newline characters to BR HTML tag. http://php.net/nl2br