using php to get two even columns of text - php

Does anyone know a clever way to create even columns of text using php?
So lets say I have a few paragraphs of text and I want to split this into two columns of even length (not string length, I'm talking even visible length).
At the moment I'm splitting based on word count, which (as you can imagine) isn't working too well. For instance, on one page I have a list (ul li style) which is increasing the line breaks but not the word count. eg: whats happening is that the left column (with the list in it) is visibly longer than the right column (and if there was a list in the right hand column then it would be the same the other way round).
So does anyone have a clever way to split text? For instance using my knowledge of objective c there is a "size that fits" function. I know how wide the columns are going to be, so is there any way to take that, and the string, and work out how high its going to be? Then cut it in half? Or similar?
Thanks
ps: no css3 nonsense please, we're targeting browsers as far back as ie6 (shudder). :)

I know you're looking at a PHP solution but since the number of lines will depend on how it's rendered in the browser, you'll need to use some javascript.
You basically need to know the dimensions of the container the text is in and using the height divided by the text's line-height, you'll get the number of lines.
Here's a fiddle using jQuery: http://jsfiddle.net/bh8ZR/

There is not a lot of information here as to the source data. However, if you know that you have 20 lines of data, and want to split it, why not simply use an array of the display lines, then divide by two. Then you can take the first half of the PHP array and push it into the second column when you hit the limit of the first.
I think you're going to have trouble displaying these columns in a web browser and having a consistent look and feel because you're trying to apply simple programming logic to a visual layout. CSS and jQuery were designed to help layout issues. jQuery does have IE6 compatibility.
I really don't think you're going to find a magic bullet here if you have HTML formatting inside the data you're trying to display. The browser is going to render this based on a lot of variables. Page width, font size, etc. This is exactly why CSS and other layout styles are there, to handle this sort of formatting.
Is there any reason why you're not trying to solve this in the browser instead of PHP? IE6 to me is not a strong enough case not to do this where it belongs.

Related

how to show short version of posts

I'm writing a blog and I want to show short versions of posts on the main page. I assume native php string functions aren't appropriate here since posts can be large and it would take long to substr all posts in loop. So, what is the common strategy here? I hope the question is clear and specific.
I don't want to shorten posts on client side with JS, that's not an option.
The solution I use is to make another field in db table with posts where I put short version of post, cuted begining or something like that.
It's faster, and better, you don't have to worry about length becouse you control it, there is no problem with evenual html tags used in context, and you can have a bit diffrent text on mainpage
I can think of two options. The first one involves you writing excerpts for your blog posts manually. Doing this, you don't have to worry about PHP at all.
If you do want to go ahead and automatically generate excerpts, I would set a upper character limit and then cut at the end of the sentence nearest the chosen limit. This approach may or may not produce good results depending on how your post is written.

Need an algorithm to find near-duplicate text values

I run a photo website where users are free to enter any tag they like, even tags not used before. As a result, a photo of a tag may sometimes be tagged as "insect" whilst somebody else tags it as "insects".
I'd like to keep the free-tagging capability, yet would like to have a way to filter out such near-duplicates. The total collection of tags is currently at 1,500. My idea is to read all of them from the DB into mem and then run an alghoritm on it that displays "suspects".
My idea of a suspect is that x% of the characters in the string are the same (same char and order), where x is configurable. I could probably code a really inefficient way to do this but I was wondering if there is an existing solution to this problem?
Edit: Forgot to mention: just sorting the tags isn't enough, as that would require me to go through the entire set to find dupes.
There are some flaws in your logic. For example, what happens when the plural of an object is different from the singular (i.e. person vs. people or even candy vs. candies).
If English is the primary language, check out Soundex which allows phonetic matches. Also consider using a crowd-sourced synonym model where users can create links to existing tags.
Maybe the algorithm you are looking for is approximate string matching.
http://en.wikipedia.org/wiki/Approximate_string_matching.
by a given word you can match it to list of words and if the 'distance' is close add it to suspects.
A fast implementation is to use dynamic programming like the Needleman–Wunsch algorithm.
I have made a blog example of this in C# where you can configure the 'distance' using a matrix character lookup file.
http://kunuk.wordpress.com/2010/10/17/dynamic-programming-example-with-c-using-needleman-wunsch-algorithm/
Is "either contains either" fine? You could do a SQL query something like this, if your images are in a database (which would only make sense):
SELECT * FROM ImageTags WHERE INSTR('theNewTag', TagName) > 0 OR INSTR(TagName, 'theNewTag') > 0 LIMIT 1;
If you really want to do this efficiently I would suggest some sort of JavaScript implementation that displays possibilities as the user is typing in a tag that they want. Not only will it save the user time to happily see 5 suggestions as they type. It will automatically stop them from typing "suspects" when "suspect" shows up as a suggestion. That is, of course, unless they really want "suspects" as a point of urgency.
You could load a huge list of words and as the user types narrow them down. I get the feeling that this could be very simplistic esp if you want to anticipate correctly spelled words. If someone misses a letter, they'll probably go back to fix it when they see a list of suggestions that isn't at all what they meant to type. And when they do correctly type a word it'll pop up in the suggestions.

Dynamically generate a table with specific rowspans in PHP?

I've been racking my brain about an easy way to generate an HTML table with rowspans with PHP that would look like this, numbers indicating each cell's rowspan:
I noticed that there is sort of a pattern in the rowspans if you go from left to right, top to bottom: 1,3,7,1,1,1,1,1,3,1,1. I'm not sure if that matters.
Also, I wonder if it's possible to use any method for the table above for a table like this:
maybe addressing your second example first is easier, but it might be adapted to the first example.
the structure of the tables in html is obtained using the rowspan an colspan properties.
try to picture the table as a closet, the rows as drawers and cells as boxes inside those drawers.
with these properties, u can "merge" the cells throughout rows and/or columns.
my approach would be to set a minimal block with the appearance and data that you need, ie your second example, and write it down in html.
the "php-cycle-the-thing-up" part would require you to use that html "template" in the loop dropping your recordset data in the td portions of the html.
in that way, i think you might get what your looking for.
on the other side, if you need the php to create the table dynamically, and merge the cells automatically in a set pattern, or even ignoring the pattern and simply adapt the structure as the data is coming, it would probably be better to look at the structure from the end.
because of the way that tables are built with html, nesting table-tr-td, building cycles with this look is tricky. have you considered simply nesting divs?
i'm new to stackoverflow, i wanted to comment but by the time i finished this, i couldn't remove the answer nor comment the question, sorry if this didnt quite help you... :S
write down, come up with the recursion formula for the table. you need to come up with the math with the least number ideally ZERO conditional statements.
it's not that hard. for a desktop, i use three columns if it detects a mobile devices, my php automatically switches to 1 or 2 columns depending upon screen size

Fill a div with text untill is full, then create another one

I am building an ebook app, and for that I have a big chunk of text (the book) and I have to divide it into N number of divs with a special class. I manage to achieve somehow a result by counting the words and after N words create another div, but words count is not a really good parameter.
Is there any way I can fill a div (320 x 480) with text and when its full create another one? In the end what I need is the full list of divs one after another.
Any hint on what should I look for is much appreciated!
Thanks
No, because you can't work out programatically how big that text is going to be when it is ultimately rendered for display.
Why not let the device itself work out how much text it can display on a page?
I don't have time to work out the specifics, but something (admittedly hacky) you could do is add a certain amount of text to the div and then check how its scrollHeight compares to its offsetHeight. This is probably going to be really slow, and you'd have to have some way of comparing it before you actually add the text: scrollHeight is always equal to offsetHeight, until the text actually overflows, and that's exactly what you're trying to avoid. Maybe some kind of temporary div? I don't know. I'll leave the specifics to you to work out. ...Or ignore completely. Whatever.

Multiple column articles in Joomla

I've got a client that requires that an article be displayed in two, sometimes three, columns in Joomla. I am fairly sure they won't be happy with having to edit 3 articles for 3 columns so the splitting would have to be done automatically.
I've done something similar before where it'll split a chunk of HTML into n columns, but have no real idea how to accomplish this within Joomla itself.
Any ideas gratefully recieved!
An alternative approach:
Use Javascript to split up the Article in several column in the browser. Here I could imagine a full-automated approach could work.
Advantages (over the first approach):
As Javascript can know, which height the paragraphs actually have in the browser (unlinke PHP), you could find the optimum split more accurately.
This can be implemented in the template php-File: you tell the template to include the js-File. So it could be made context-dependent,E.g.: If the left column is collapsed (because there are no modules in it), tell the JavaScript-File to initialize to 3 columns, else 2 columns.
However, have in mind that it should rest usable for those who have Javascript disabled.
This doesn't seem to be easy.
At first thought this should be an CSS attribute, but if it exists, it is part of CSS 3.0 and as such only understood by modern browsers (if at all). But I didn't find any way to do this in CSS.
So you actually have to modify your HTML code. I would propose the following:
A Button (editor-xtd plugin) that splits the article into several parts, each one for one column, showing a dotted line in the editor box (similar to the "read-more"-Button).
E.g. it inserts in the article: (you will have to define hr.column in /templates/system/css/editor.css).
A (content) plugin that creates the multiple colum-style,
E.g. replacing the hr-Tag with table or floating divs.
This way, it is half-automized, without mangeling in the Joomla! files but only adding to extensions to it.
the CSS 3 rules for multi-columns are:
-column-width
-column-gap
-column-rule
-column-count
with the vendor label (-moz, -webkit) before.
More info at http://www.css3.info/preview/multi-column-layout/
I would use css and tell the people with Explorer to change browser! (i'm jocking of course)
Otherwise javascript is the way like said before. This script should do (not tested) http://13thparallel.com/archive/column-script/
This should be done through the template, some PHP coding is involved.
One of our clients asked us to do the exact same thing before, and we have done it through template. Note that for very small articles we increased the font in order to split the article into 3 columns.

Categories