How to contain text from breaking elements? - php

This is a very simple question, but embarrassingly enough I am not sure how to implement this :\
I have div elements with text boxes inside them for users to write comments,replies etc... Very standard concept. The div that contains the text is a certain width. If someone where to write something with out using any spaces, instead of breaking down a line when it hits the right edge of the div, it just keeps going and breaks the element.
I am just wanting to know how to make it line break when it hits the edge spaces provided by user or not.
thanks in advance

There is a word-wrap CSS property that will force long words to wrap:
word-wrap: break-word;
You might also want to look into overflow.

Hmmmm...This is an interesting problem. I'm thinking what I would do would be to implement a JavaScript function that would "read" the length of lines and if a single block of writing is longer than the max length, I would grab a substring of (max length - 1), append a dash (-) to the end of it, add in a line break, and proceed from there.

Related

Problems with LONG NAMES (PHP): how to place 3 DOTS (…) at the end of a fixed SPACE LENGTH

We are having difficulties displaying LONG NAMES on our website and would need help urgently.
We actually need to place IMAGE NAMES under IMAGES that have a FIXED WIDTH. Because image names are sometimes longer than the image width, we don’t want to display the complete name on too many lines because it would not look nice.
We then either want to display the names on 1 (or 2) line(s) maximum (depending on the page).
When the NAME IS LONGER than 1 (or 2) line(s), we would like to CUT THE IMAGE NAME and place 3 DOTS (…) at the end of the line so that the third dot would reach the end of the image.
We have tried to find a solution, but defining a string length is not one of them, because if we define a limit of for example 10 characters then put the 3 dots afterwards, it will not solve the problem since all characters don’t have the same length:
Example 1: AAAAAAAAAA
Example 2: IIIIIIIIII
This solution is inappropriate since the 3 dots will never be placed at the same place (at the end of the image)
Does anyone have a solution?
We would really appreciate your help.
Thanks a lot.
PHP, being a server-side language, has no way to know how the browser will render the text. A CSS solution is way more feasible:
div.picture-name{
width: 100px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
See it in action.
P.S. Can't guarantee IE support for text-overflow: ellipsis.
I know this is not the best option but you can place each character within a div or span then you will probably have a control over length of the sting.

How to I add a line break after each full stop in php, when pulling text from the database?

I have text within one of my tables which is a long description, but the text is all clumped together. I need a way for PHP to find each full stop and add a line break after it, so I can treat the description as a list.
A better solution would be to add an tag at the start of each sentence, and have each sentence finish with a so I can actually add bullet points.
Thanks.
You can use the str_replace function to replace each occurrance of a full stop with a full stop plus a line break. If you are rendering HTML, a line break is probably the <br/> tag.

php character limits (trim an html paragraph)

We have our own blog system and the post data is stores a raw html, so when it's called from the db we can just echo it and it's formatted completely, no need for BB codes in our situation. Our issue now is that our blog posts sometimes are too long and need to be trimmed.
The problem is that our data contains html, mostly <font>, <span>, <p>, <b>, and other styling tags. I made a php function that trims the characters, but it doesn't take into account the html tags. If the trim function trims the blog it should not trim tags because it messes the whole page. The function needs to be able to close the html tags if they're trimmed. Is there a function out there that can do this? or a function where I could start and build from it?
There's a good example here of truncating text while preserving HTML tags.
There is strip_tags which gets rid of all HTML tags but other than that there isn't much.
This is not an easy thing by the way, you have to actually parse the HTML to find out which tags are left open - that's the most robust approach anyway. Also, don't use a regular expression.
The right solution is to not store display information in your database layer.
Failing that, you could use CSS overflow properties: print the whole post, and then have the display layer handle sizing it to fit. This mitigates the problem of having formatting information in your database by putting the resizing (a display issue, not a content issue) into the display layer as well.
Failing that, you could parse the HTML and "round up" or "round down" to the nearest tag boundary, then insert the tag-close characters necessary to finish the block you were in.
Another option is to iframe the content.
I know this isn't the best way to do it programatically, but have you considered manually specifying where the cut should be? Adding something like and cutting it there manually would allow you to control where the cut happened, regardless of the number of characters before it. For example, you could always put that below the first paragraph.
Admittedly, you lose the ability to just have it happen automatically, but I bring it up in case that doesn't matter as much to you.

How to get sentences from the website html

Hello I want to extract all sentences from a html document. How can i perform that? as there are many conditions like first we need to strip tags, then we need to identify sentences which may end with . or ? or ! also there might be conditions like email address and website address also may have . in them How do we make some script like this?
It's called programming ;). Start by dividing the task in simpler sub-tasks and implement those. For example, in your case, I'd design the program like this:
Download and parse the HTML document
Extract all text content (pay special attention to <script> and <style> elements)
Merge the text content to one long string
Solve the problem of finding sentences in a string (likely, just parse until you find a stop character in ".!?" and then start a new sentence)
Discard false positives (Like empty sentences, number-only sentences etc.)
First you should strip certain tags which are inline formatting elemnts like:
I <b>strongly</b> agree.
But you sbhould leave in block-level elements, like DIV and P because there are even stronger delimiters than . ? and !
Then you have to process the content in these block level elements. Typically there are navigation links with one word, you might want to filter them out later, so it is not the right choice to strip away the block structure of the document.
At this point you can safely use the regex pattern to identify blocks:
>([^<]+)<
When you have your blocks you can filter out the short ones (navigation elemnts) and strip the big ones (paragraphs of text) using your sentence delimiter.
There are interesting questions when a fullstop character signals an end of the sentenct and when is it just a decimal point, but I leave that to you. :)

I'm acquiring a VARCHAR variable through php, and want to show it using HTML/CSS. How do I auto format it so that it isn't one long sentence

I basically want to automatically add line breaks to a VARCHAR variable I acquire from a mysql database trough PHP, so that it shows right. At the moment when I show it trough HTML, all it does is give me a long sentence.
I imagine this can be done with CSS, but overflow: scroll just makes it scroll to the right and left. Use of javascript or JQuery is also accepted.
If it really is a long string with no spaces or line breaks then the word-wrap CSS property can be set to break-word. There are some examples on the MDC page. But, like others have said, if this is a normal sentence with spaces in it then a browser will wrap it automatically providing it's not in a pre block. If its plain text with line breaks that you want preserved then you can either convert the \n to <br> like elusive suggested, or just place it in a pre block to preserve its text formatting.
by any chance are the spaces in this long string eg. non breaking spaces? If so then just replace them with normal spaces.
if not then please show us some of this html that is generated and the code that generates it.
I think PHP's nl2br-function is what you are looking for. It converts any linebreaks (\n) into html-breaks (<br />). Alternatively, you could take a look at the white-space CSS command.

Categories