Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS 

Watch a log and/or search for a certain term

If you're developing on a server that has all PHP errors turned off, you can still watch the error log (if you have access to this file). For example on a dreamhost account you could try this:

tail -f /home/account/logs/website.com/http/error.log

As the errors pour in, you will see them come by. Change 'account' to the username you have on dreamhost and website.com to the domainname you host there. If the errors come in large amounts you can filter them by using grep:

tail -f /home/account/logs/website.com/http/error.log | grep -i "search_term"


Now only errors containing 'search_term' will be shown.

Quickly download the PEAR libraries

To get a headstart for your website you can use the PEAR libraries. The following script downloads a couple of the most handy libraries, and also adds some symbolic links to the newly downloaded files.

#!/bin/bash
pear config-create $HOME $HOME/.pearrc
pear config-set php_dir $HOME/pear/lib

pear install -a PEAR

pear channel-update pear.php.net
pear install -a DB Mail
pear install -o Auth Auth_SASL Cache_Lite File
pear install -o HTML_QuickForm HTML_TreeMenu
pear install -o HTTP HTTP_Request
pear install -o Mail_Mime
pear install -o Log Pager
pear install -o Translation2-beta XML_Serializer-beta

ln -s ~/pear/pear ~/bin/pear
ln -s ~/pear/peardev ~/bin/peardev
ln -s ~/pear/pecl ~/bin/pecl

Replace old style PHP tags to new style tags

Sometimes you have to work with scripts that contain old-style PHP tags. This little snippets fixes those scripts, so they use the new style PHP tags.

find * -type f -exec perl -i -wpe 's/<\?php/<\?/g' {} \;
find * -type f -exec perl -i -wpe 's/<\?/<\?php/g' {} \;

Check the syntax of a bunch of PHP files all at once

Will find all files ending with 'php' and execute the syntax check of php for every found file. Useful if you quickly want to see which php file contains a parse error.

find ./ -type f -name \*.php -exec php -l {} \;

Search for files modified the last ... days

Searches for files modified up to 4 days ago. Change the 4 to whatever you desire.

find ./ -type f -mtime -4 -exec ls -al {} \;

Find files with a certain extension, where the files contain a certain search term

Searches the current directory and deeper for files ending with the 'php' extension, where the file itself contains 'search_term'. Useful if you're searching a large website with lots of images and you only want to find a certain function or variable.

find ./ -type f -name \*.php -exec grep -il "search_term" {} \;

Find files with certain extensions

Searches the current directory and deeper for several extensions. In this example both files mathing 'php', 'html' and 'tpl' match the search.

find ./ -regex ".*\(php\|html\|tpl\)$"


As an alternative you could also use:

find ./ -iname "*.php" -or -iname "*.tpl" -or -iname "*.html"

Find files with a certain extension

Searches the current directory and deeper for all files having an extension 'php'. Change this to the extension you're looking for.

find ./ -type f -name \*.php
« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS