Determing div layout dimensions php - php

I have three div's which are being filled with dynamic text from a database. The div #container is a fixed height and width where the text inside wraps. The three divs are different font sizes. Any of the three div's could have enough text to exceed the container size. I need to determine if the text exceeds the container size and at which letter in which div it occurs. The extraneous text will then be wrapped in something like <span class=hide">text here</span>
<div id="container">
<div id="first"><?php echo $arr['first'] ?></div>
<div id="mid"><?php echo $arr['mid'] ?></div>
<div id="last"><?php echo $arr['last'] ?></div>
</div>
I'm thinking this is impossible to do in PHP as the styling is done client side. Maybe there is a way to fake it? Though that could get ugly really fast.
I'm trying really hard not to do it in javascript because this calculation will be done about 10 times per page viewed. Please don't tell me it's impossible to do in PHP, there's always a way.
Any ideas?

Just in case you decide that client-side makes more sense for you I put together a fiddle. I realize you want to avoid client-side, but you mentioned this would be happening ten times which honestly is very little these days with how much js speed has increased in browsers. It is also a much simpler problem client side.
http://jsfiddle.net/JSRtk/
Basically you detect if the container is overflown. If so you display a 'read more' button. When clicked it will expand the container to show all text and go away.
$('#container > div').each( function() {
if (checkOverflow(this)) {
console.log('overflow detected in ' + $(this).attr('id'));
$(this).after('<p>Read more...</p>');
}
});
$('p').live('click', function() {
$(this).prev('div').css('height', 'auto');
$(this).hide();
});
function checkOverflow(el)
{
var curOverflow = el.style.overflow;
if ( !curOverflow || curOverflow === "visible" )
el.style.overflow = "hidden";
var isOverflowing = el.clientWidth < el.scrollWidth
|| el.clientHeight < el.scrollHeight;
el.style.overflow = curOverflow;
return isOverflowing;
}

Do you have to wrap it in a span for a purpose (i.e. crawlers/seo)? If not you could either set the CSS on the div with a fixed width to have overflow hidden, alternatively, you could figure out how many characters roughly fit in that width (count them) then use strlen() and substr() like so
<?php
$string = "This is a string thats too long to fit";
if(strlen($string) > 20)
echo substr($string,0,20);
else
echo $string;
?>

There is no way for you to calculate the size of a display element in PHP since it is run on the server and not on the client, and it's the client that renders the HTML.
If you have the same container size every time and the same font and font size and styles and everything, you could probably estimate a number of character and cut it off in PHP at that number of characters using substr. But even then, unless you build a table of character sizes or use a monospaced font, there is no way to reliably do what you want.

Related

Formatting fraction fir display

I'm formatting fraction with MathJax and are having problem displaying it properly.
$disp = '<h1>$${{10 \over 9 }} of 99 $$</h1><br>';
echo $disp;
For some reason, i cannot get a space before and after the word 'of'. Any pointers is greatly appreciated. Thanx in advance.
This is better handled as
$disp = '<h1>$${10 \over 9}\text{ of }99$$</h1><br>';
as the accepted answer does not get the font or spacing for "of" correct.
It also seems that you may be using <H1> simply to get a larger size. If so, that is bad practice, as <H1> is a structural element indicating a top-level heading (not a layout element for a larger size). Unless this expression really is a top-level heading, you should not use <H1> for it. For example, people using assistive technology like screen readers often are given a list of the headings so they can quickly jump to the important starting points of your page, so if you make all your expressions be headings, that will complicate their already difficult task of navigating your page.
Layout should be controlled by CSS, so you could use a <div> with a class around your display math if you want to size it. Or you could use one of the TeX macros like \Large or \LARGE to make the math larger from within the expression. But don't use a heading indicator unless it really is the start of a new section of your page.
Here are some examples:
.dmath {
font-size: 200%;
}
<script src="https://cdn.jsdelivr.net/npm/mathjax#3/es5/tex-chtml.js"></script>
Bad:
<h1>$${10 \over 9} of 9$$</h1>
Better using CSS and <code>\text{}</code>:
<div class="dmath">
$${10 \over 9}\text{ of }9$$
</div>
Better using <code>\LARGE</code> and <code>\text{}</code>:
$$\LARGE {10 \over 9}\text{ of }9$$
<br><br><br><br>
Usually, \ keep the space between letters.
$disp = '<h1>$${{10 \over 9 }}\ of\ 99 $$</h1><br>';
Reference - Spacing in math mode

automatic subtract words when page resized or zoomed

i would like to know how can i make my title in divs automatically substracted to a certain number to stay fitting the div when resizing window or zooming website as youtube.com do ?
im substracting words like that
$string = (strlen($title) > 13) ? substr($title,0,26).'..' : $title;
echo "<div>";
echo $string;
echo "</div>;
But i dont know how to do this automatically so it can substract more letters to fit the div.
now what i have is when page resized or zoomed the title stay long and will out of the div.
how can i fix this with php or javascript thanks. i tried looking how youtube doing it but no luck.
You can use CSS3 text-overflow: ellipsis like this:
<div style="text-overflow: ellipsis;width:5%;overflow: hidden">
Some text
</div>
Try with the fiddle: http://jsfiddle.net/bBTaU/
Resize the window and see
EDIT
Not all browsers support CSS3, you can use a plugin like this http://dotdotdot.frebsite.nl/ to extend the support in old browser

Aligning text inside an <option> tag

I have a select tag with a lot of options. Every option has a name and a date which both should be printed in every <option>.
I want to align the list like this:
Name Date
Name Date
Name Date
Since every name has different length, I wrote this code:
//Start of the option text//
//Always print 20 characters, even if the name is longer//
print ">".substr($eventsArr[$j]->GetName(),0 ,20);
//If the name is shorter then 20 chars//
if(strlen($eventsArr[$j]->GetName()) < 20)
{
//Add missing chars (20 - the length of the string) with spaces//
for($t = 0; $t < 20 - (strlen($eventsArr[$j]->GetName())); $t++)
{
print "&nbsp";
}
}
print "" .$newDate."</option>\n"; //Then print the date//
I'm getting the correct amount of spaces completed. But as you can see, the alignment is not 100%:
I'm guessing its because every letter has a different width in pixels. So... Is there any way of doing this kind of alignment ? Thanks.
Just use a Monospaced font for this. It's what they were designed for.
Option elements were not meant to be formatted that way. Using a monospace font would be the way to achieve alignment, but that would make the text look ugly, and monospace fonts are less readable, too. Moreover, not all browsers honor font family settings for option elements; for example, IE 9 does not.
The workaround, or alternative approach, is to use a set of radio buttons instead of a select element. Then you can use a table:
<table>
<tr><td><input type=radio id=a1> <label for=a1>Name</label>
<td>Date
...
</table>
This handles the alignment, and this also lets you specify the font used for the labels in a manner that works across browsers.
If there is a large number of alternatives, you might consider setting a height and vertical scrolling for the table. But it might be better to let users just scroll down the page as needed, instead of having two levels of scrolling.

PHP/MySQL Limit Characters

Below is a preview of what I am dealing with:
The heading, the text, and the image is all dynamically created based on the user. They can choose if they want to add a picture, and they can choose what text to put in the heading and the main content.
There cannot be any scrolling, so it has to be visible and cannot go past the bottom of that white.
My question is... how can I limit the text of the body?
If the heading is large and goes down to two lines, and there is a picture, then in order to stay in the lines and not go past the white it has to be limited to a certain amount of characters.
But another user could decide to not have a picture and keep the heading in one line, so that user will have more text to write so the limit should be different.
I guess the confusing part for me is like.. what if a user has no picture and has a short heading, and creates some really long text to fit the size, but then later on decides to add an image.. then that long text will now no longer fit. So now.. I can't limit the text because it's already there.
I hope that makes sense. If anyone could help me through this and give me some ideas I would really appreciate it.
Use this for whatever the user input is for adding text. It could limit the characters they use just change the 250 value.
<script>
function countChar(val){
var len = val.value.length;
if (len >= 250) {
val.value = val.value.substring(0, 250);
}else {
$('#charNum').text(250 - len);
}
};
</script
<textarea id="field" name="description" onKeyUp="countChar(this)"></textarea>
its not only the text you need to limit but also the image thumbnail so that affect the style of your webpage
To limit the number of string .. i would you use a script form here php trim a string
function trim_word($str, $length, $suffix = '...')
{
$len = strlen($str);
if ($len < $length) return $str;
$pattern = sprintf('/^(.{%d,}?)\b.*$/', $length);
$str = preg_replace($pattern, '$1', $str);
$str = trim($str);
$str .= $suffix;
return $str;
}
it trim the text and makes sure it always ends with a word ...
You can use http://phpthumb.sourceforge.net/ to generate thumbnail of fixed size all you need to do it set your desired height and width
There are a few different options to consider. You may want to limit the amount of text the user can enter for starters, to ensure it doesn't overflow.
One thing I would probably do is find the maximum amount of characters you're comfortable with on the page, and use substr on the output from the database to ensure that it never displays more.
http://php.net/manual/en/function.substr.php
You could have a "more" link that way the visitor could read more if they want, but it doesn't break the layout. I would use basic if statements for the logic (if picture exists, trim text to this, if not etc).
Hope that helps.
Surround the entire thing with a container div of the fixed height which you desire with no padding, and inside that place another inner div with no margin and no fixed height, then as the user changes the content create a javascript function to check if the inner div is < the container div; and if not then do not allow the user to make that change - this way you are attacking the problem directly.
<script>
function checkDivs() {
if(inner.style.height >= container.style.height) {
//prevent change
}
}
</script>
<div id='container'>
<div id='inner'>
//User-defined content
</div>
</div>
This function would be attached to whatever GUI the user would edit the content through, as for how to prevent the change, I'd have to know more about your program.

substr and strlen explanation

Ok guys, this question is related to my previous one.
If I have set $textlimit = 500; that will limit my text to 500 characters.
Is there any way to "avoid" text limit, and then onclick function load rest of it?
For example, if I set:
$textpart = substr($fulltext, 0, 400);
$textpart will only contain 400 characters of string.
My question is, how to declare variable, which will contain the rest of the text which is much longer than 500 characters?
Example of variables:
$fulltext //Contains full text, but is limited to 500 characters.
$textpart //Contains part of the text, substr 400 characters out of 500.
$textrest //This variable to "hold" rest of the text, after 400 characters of $textpart.
Like I've asked in previous question, I wanted to make expand and collapse button, I now know how to do that, but I don't know how to divide text.
Form would go like this:
Random text here(400 characters long)
Random image for expand
After declared onclick function I, load rest of the text (Over 500 characters).
Random image for collapse
After declared onclick function collapse and return to previous state - citation 1.
I hope I explained my question the right way. I would really appreciate any kind of help, if I can choose, I would like just basic explanation on how to that, because I want to learn that, not copy/paste solution (it is easier, but I will not learn much).
Thanks in advance.
$textrest = substr($fulltext, 400)
$fulltext = substr($fulltext, 0, 500);
$textpart = substr($fulltext, 0, 400);
$textrest = substr($fulltext,400,strlen ( $fulltext ));
If I understand you correctly you want to show the user an initial page that shows only the first X characters and then show all the characters when the users clicks on the text.
There are three strategies to do this. From easy to hard:
Output the shortened text and include a link that will reload the whole page but with the whole text
Output all the text and use css and JavaScript to hide/show any overflow
Output the shortened text and perform an Ajax call to load the extra characters and append
Options 2 and 3 require the use of client side JavaScript and are therefore not pure PHP solutions.
Option 1 is a matter of adding a $_GET variable, e.g. ?expand=para1, to your url and expanding the text identified in PHP by $_GET['expand'].
Do not make the mistake of thinking PHP is still running on the page in the browser. Only JavaScript can run in the browser on the web page. (Not strictly true I know, but true enough in reality.)

Categories