The homepage of Chris Tate-Davies
Posts tagged PHP
Accessing the Zend_Application from a page
Jul 27th
If, like me, you need to access some Zend_Application resources (such as multidb resources) – then you need a reference to the Zend_Application and the bootstrap. I use this:
if (null == $this->_application)
{
$this->_application = Zend_Controller_Front::getInstance()
->getParam('bootstrap');
}
Online regex -> php tool
Jun 22nd
I’ve found an excellent online tool, where you can give it a regex, and it will spit out the PHP syntax for use with preg_replace, preg_match, etc.
I’m forever getting ‘Unknown modifier’ errors when trying to insert regex’s into my PHP code. This sorts it out for you.
Remove svn folders from Ubuntu
Apr 9th
When backing up a source code working copy, you don’t always want all the little .svn folders everywhere.
You can “export” the working copy, or if you forgot, you have remove them all.
Change the the folder containing the working copy (this is very important*)
cd /home/username/www/sitename/
And then remove them all, recursively with this command:
find -name "\.svn" -exec rm -rf {} \;
This will find all .svn files/folders in the current folder, and remove them and everything in them.
*if you do not ensure you are in the right folder, this command will remove all recursive folders, so if you are in the web root, it will remove all the svn information from all your sites… This is very important (that’s twice I’ve said that, so it must be)
Regex – “The” searching
Mar 26th
Say you have a list of movie titles, and you want to either sort them, or search through them, and some of them have “The ” at the start, for example:
- The Simpsons
- Simpson Street
When doing a MySQL search:
SELECT * FROM movies WHERE title LIKE "The Simp%";
Would only return the first row. But if you are working in a company where there is no standard set, the movie title could be formatted as “Simpsons, The” – and then, it won’t be found.
To solve this, you could replace the “The ” letters with blank, and then sort out the field contents during the query:
$str_query = preg_replace("/(title like \"(the )(.*)\%\")/i",
"REPLACE(LOWER(title), \"the \", \"\") LIKE (\"$3%\")",
$str_query);
This will change :
"(title LIKE "The Simpsons")"
to,
"(title LIKE "Simpsons")"
But, the (the) in line 2 tells PHP to only replace it if starts with “The ” (case insenstive).
However, what if you want to search for “the” (not sure why you would…)
You need to do a negative lookahead, to tell the expression to only carry on, if the search phrase is not exactly “the”
if (preg_match("/(title like \"(?!the)(.*)\%\")/i", $str_query)) {
The (?!the) is the readahead.
(.*) matches any string but it is greedy and you have to be carfeul that it doesn’t just accept everything to the end of $str_query. (but its okay in our case, as we are looking for \% (the LIKE wildcard))
After all this, we can run:
SELECT * FROM movies WHERE $str_query;
But what about sorting? All the titles beginning with “The” will appear in the T section. Whereas really, we want the Simpsons to appear in the S section.
Add an easy ORDER BY clause here:
SELECT * FROM movies WHERE $str_query ORDER BY (REPLACE(title, "the ", "") ASC;
Sorted!