Dynamic text background with padding at end of line break, character count - php

I want to close out my old question start a new one with a better sample photo.
I am trying to add a transparent png background to some dynamic text with line breaks and give it 15 px padding at the end of each sentence when the line breaks. But this text does not have the br tag in the html code. I don’t want the background to be a square box. I uploaded a sample of what I have and what I am trying to get. I heard there is a php script that can count characters like strlen, count_chars, substr_count. Can anyone help me. My css is below. It’s a span tag around dynamic text that changes. This is a Drupal Views Slideshow and it changes every few seconds, the photo and the text.
Below is the code in Drupal Views I used Tokens for the text that needs the padding the Token is called 'text'.
.bluebackground
{background-image:url(images/transparentback.png);}
<div id="textbox">
<span class="bluebackground">[text]</span>
</div>

I'm not sure why you would need PHP, instead just use CSS.
.bluebackground {
background-image:url(images/transparentback.png);
padding: 15px; /* add 15px padding around entire box */
}

Related

Calculate length of user input to prevent overflow when outputting in div

I am reading a string my user is inputting via PHP and I need to spit it back out in a div tag. This div tag has a width of 500px. If the user enters a word that is too long, the word will overflow the container. If the user enters two words that are almost two long, it will split into two lines.
My question is how do I determine if a word is too long or not? I have tried setting a character count, which is not an accurate representation of length as certain characters (ie W and I) have different widths. Is there a solution?
My current algorithm is to break the user input into chunks, each of 40 characters, and output it.
If you want to still implement your character count mechanism you can; you just need to make sure your text is mono-spaced (same width). To do this you can just add <pre></pre> around your text block; this can also be accomplished with <code></code> and <tt></tt> but if you want a simple CSS solution you could use.
<style>
.myclass { word-wrap:break-word; }
</style>
<p class="myclass">some text</p>
Usually you shouldn't use PHP for things like that. Try CSS instead:
.break { word-wrap: break-word; }
will do the trick
you can simply break-up words that are larger than, say, 20 characters - into chunks, using the <wbr> tag. Here's some more info: http://motyar.blogspot.co.il/2011/07/tell-browser-they-may-break-your.html

Keep two first text lines, remove everything else

In a <div> I have some text. Because of the div-width the text is shown in multiple lines. E.g. the following code:
<div>text01 text02 text03 text04 text05 text06 text07 text08 text09 text10 text11 text12</div>
might be shown in e.g. four lines:
text01 text02 text03
text04 text05 text06
text07 text08 text09
text10 text11 text12
I wish to keep only the first two lines, and if further lines are present they must be removed and replaced with the text line ... as a new (therefore third) text line.
In other words: I wish to find the second line break (if present) and replace all text after this point with a text line saying ....
So, if I have two lines of text, nothing is changed:
text text text
text text text
But if I have more than two lines like above, I will get this:
text01 text02 text03
text04 text05 text06
...
Any good advice?
You should do that in css and if necessary add javascript.
In css you can set:
.two-line-div {
max-height: 3em; /* or whatever adds up to 2 times your line-height */
overflow: hidden;
}
That will reduce the box to the desired height.
If you always want to show ... (if the content is always more than 2 lines), just add an element with the three dots after your div.
If you want to add another line with ... if the content is bigger than what you are showing, you would need javascript to calculate the original height, see if it is more than 2 lines and add / show an element dynamically if it is.
Note that a css solution does not remove anything, all lines are there, they are just not visible.
There is a pure CSS Solution working in most of the modern browsers (some older firefox versions didn't support it):
div {
overflow: hidden; /* "overflow" value must be different from "visible" */
text-overflow: ellipsis; /* the magic dots...*/
height: <yourHeightValue>
width: <yourWidthValue>
}
Doing something like this in PHP could be a bit more complex, depending on what the DIV contains (nested HTML?, what exactly is a "line" for you -> a HTML break <br>, a line-break \n)? In most of the cases the PHP solutions split after a defined String or Word length. You can find quite a few examples for this kind of text limitations, this one is a complex solution which can handle html tags too.
You can use explode() to split your string (the contents of the div) into an array. Use <br> or /n as the split token. Then you can replace the contents of the div with the first two elements of the array.
$content = 'Hello<br>World<br>Other<br>Stuff';
$lines = explode('<br>',$content,2);
echo '<div>'$lines[0].'<br>'.$lines[1].'<br>...</div>'

phpdocx, docx to html converter returns broken span tags

I'm successfully using the free version of phpdocx php library to convert docx files into html files.
The problem is that the output often contains lines that look something like the one below:
<span style=" font-family:;">This line is</span><span style=" font-family:;">completely b</span><span style=" font-family:;">roken</span><span style=" font-family:;">b</span><span style=" font-family:;">ecause of the random span tags
And the same seen in the browser:
"This line iscompletely brokenbecause of the random span tags"
I tried to fix it afterwards when cleaning the html output by removing any unnecessary span tags and placing spaces around each one. But of course it only fixes some instances, and actually breaks elsewhere. With the above line it would outcome:
"This line is completely b roken b ecause of the random span tags."
Currently the only way to prevent the span tags appearing is to select and remove any styles associated with the sentence in Word. It would be acceptable if you were able to spot the problems in Word already, but you can't really.
Do you have any experience with phpdocx and whether there is a solution to this?
Thanks.
Blah! The problem was my own bit of cleaning up. Namely the following bit of code:
$result = preg_replace('~>\s+<~', '><', $result);
Case closed.
Thanks.

Display Paragraph's text with Diagonal Indent

I wonder if there is a way to display paragraph text with diagonal indent to be some thing like that!
Keeping in mind that this text is written in WYSIWYG editor (Contains html tags).
I was thinking if there is a way to count the words within the paragraph excluding html tags and then making some equations to increase the indent of the text every line by jQuery or Javascript.
Is there any suggestions to do that ?
You can skew the containing div
.holder{
transform:skew(-40deg);
}
<div class="holder">
<span class="rotate">Just </span>
<span class="rotate">Like</span>
<span class="rotate">This! </span>
</div>
and then unskew each word inside it.
.rotate {
transform: skew(40deg);
}
https://jsfiddle.net/dcst94sv/5/
There's a very easy way to do this with CSS. Create a list. Then use li::before to add left-side padding to the list items. Set the li::before element to be a tall and thin block floated to the left. Each one will create left side padding for its parent list item and all those below it.
Like this:
li::before {
content: "";
display: block;
float: left;
height: 50px;
width: 10px;
}
<ul>
<li>sancti et dilecti</li>
<li>viscera misericordiae</li>
<li>benignitatem </li>
<li>humilitatem</li>
<li>modestiam</li>
<li>patientiam</li>
<li>caritatem</li>
</ul>
To count the number of words within the paragraph excluding HTML tags, use:
$tagless_content=strip_tags($content);
str_word_count($tagless_content);
Update
Here is code to increase text-indent via jQuery
jQuery.fn.stripTags = function() {
return this.replaceWith( this.html().replace(/<\/?[^>]+>/gi, '') );
};
then use the String.length JavaScript property
var len = $('<p>').stripTags().length();
for(var i=0;i<len;i++)
jQuery('<p>').css('text-indent',+i+'px');
Reference
Strip tag via jQuery
strip_tag PHP function
str_word_count PHP function
There's no straight-forward solution that I am aware of, since, as you indent each line of the text more and more, the length of space that each line can take up will decrease, creating new lines.
For example:
TEXTTEXTTEXTTEXT
TEXTTEXTTEXTTEXT
TEXTTEXTTEXTTEXT
TEXTTEXTTEXTTEXT
TEXTTEXTTEXTTEXT
Post-indentation:
TEXTTEXTTEXTTEXT
TEXTTEXTTEXTTEX
T
TEXTTEXTTEXTTE
XT
TEXTTEXTTEXTT
EXT
TEXTTEXTTEXT
TEXT
This problem will exist if (1) you are processing lines created due to word wraps, and (2) if you detect all of the lines at once and then do all of your indents (as opposed to an algorithm that updates the <p>'s text. If the font family is mono-spaced font, then this can be adjusted for.
Best-case scenario is that these are <br>-terminated lines, in which case this would be very easy.
Slightly more difficult would be doing this with a mono-spaced font.
Worst-case scenario, describe above, would require searching for the first line that is not indented, then indenting it, updating the <p>'s text, and then repeating the process until the text is completely gone through or if the amount of indention exceeds with width of the <p>.
I would suggest asking your question again, providing the following information:
- are the lines terminated with <br> tags, or are they word-wrapped? If it is word-wrapped, is the font mono-spaced or variable-width?
After many Searching stuff I found something useful http://www.csstextwrap.com/examples.php
I think after some modifications it will fit my requirements. thanks for your highly appreciated Responses.

Center align plain text (php)

I have been asked to come up with a solution which would allow our users to create their own custom javascript dialog text. However we need it to be centered.
At the moment, we have a textarea, which the user pads using spaces and tests with a preview button. I would like to change this to allow for the text to be center aligned automatically. I think this would mean just adding the spaces myself line by line in the backend, and also adding in the correct line breaks.
The only way I can think of doing it, is getting the longest line as int, and then subtracting subsequent lines from it, and diving the result by two, and tacking that many spaces on front and back.
Is there a cleaner more elegant way to approach this problem? Are there ways of aligning text actually inside the dialog?
I had considered something like TinyMCE, but I think it's a little overkill, for what is essentially a 150 character, 4-5 line string.
On the PHP side, you can do this.
$lines=array();
foreach (explode("\n",wordwrap($str,$len=80)) as $line)
$lines[]=str_pad($line,$len,' ',STR_PAD_BOTH);
echo implode("\n",$lines);
The Javascript version should be easy to write.
This page has a useful javascript function for center-aligning a string using padding. Assuming you're displaying plain fixed-width text (using a <pre> tag or similar) then you'll need to get the length of the longest line and pad accordingly. If not, it's just a matter of setting the css: #myDiv { text-align:center; } on the div containing the text.

Categories