Crop text without splitting words

An example of some code that crops text without stopping part way through a word.

$description = $crop_desc ?
implode(" ", array_slice (explode(" ", $details->Description),0, $crop_desc)).'...'.$morelink
: $details->Description;

Joomla version numbers

I had to consult the Wikipedia for more information on the Joomla versions recently to clear up a few things.

I've obviously missed a few things as I was expecting 1.8 to be the next release but I started to see some talk of version 2.5.

There were some proposals and a vote but both options still don't seem to fit in with my view on version numbering. I always thought the first number should be a major release, the second number a minor release and the third number a bug fix. It seems daft that you should end up having a vote to try and make sense of the confusing numbers as it should really have been quite simple.

 

Find files in Unix that were modified n days ago

To find all PHP files that were modified on a linux server in the last 7 days you can do the following

find . -mtime -7 -name "*.php" -print

Wrap an image tag with a link in PHP

I found the following bit of code useful for automatically creating Joomla lightbox images from image tags.

echo preg_replace('`.+?)src="/([^"]+)"(?.+?)>`','<a class="modal" href="/$2">', $text); 

In this example I've used the Joomla modal behavior but you can easily alter this for Jquery using rel or something similar.

Virtuemart queries

Really Virtuemart? Putting url's inside SQL statements isn't such a great idea. Its possible that its faster but I wouldn't have thought so and considering the queries Virtuemart does anyway it won't make any difference.

Part of the work I've been doing is creating an optimised version of Virtuemart and its little things like this that I like to tidy up.

Javascript to close popup window and load parent

Whilst looking at authentication applications I found this piece of code handy

window.opener.location.href = window.opener.location.href;
if (window.opener.progressWindow)
	window.opener.progressWindow.close()
window.close();

Regex to remove non ascii characters

Remove non ascii characters from a string with the following rgex

$new = preg_replace("/[^\x9\xA\xD\x20-\x7F]/", "", $old);

Regex to convert urls to links

A handy PHP regex to replace urls in a section of text to links.

$text = preg_replace('#(?])((http|ftp)s?://[^<>\s]+)#i', '\\0', $text ); 

Joomla sh404sef component

sh404 is an excellent component for Joomla that enables flexible URL rewriting. There are however a few areas where you need to pay particular attention in order to ensure it works correctly.
 
In the backend you will find SEO urls and Custom urls. In the database, the difference between these is simply the date that the custom url was created. Non custom urls have a default date.
 
The first url generated for the non seo link is the url that is used.
 
If running on your localhost with a non standard port, go into the advanced options and enable the setting.
 
If multiple seo urls exist for the same non seo link then it may be wise to find where the incorrect ones are being generated. You may find you have issues with the itemid.
 
You can turn off the redirect from the non seo link to the seo link in order to assist with debugging.
 
Most of the points relate to sh404 for Joomla 1.5 however most of it should hold true for later versions.
 

Pro PHP Refactoring

I borrowed a copy of Pro PHP Refactoring recently and while it started out quite promising its not held my attention. I really liked the ideas it suggests and value the process of refactoring over "starting again" but I just found that a lot of the content was very "samey".

Its reasonably well laid out and provides good examples but I found myself struggling to concentrate on a lot of it and reckon that it could have been done a lot more succinctly. Many of the example ideas and processes could be combined in to one process.

Its another Apress title which you can find out more about on the Apress site here.