Hints and tips

These are some helpful hints on various topics.

 

1 2 3 4 5 6

Regex for getting the content of given span by id or class

Here's a regex that extracts content of span with given id or class from an HTML text contained in $data variable:

$regex = '/\<span\s*class=[\'"]?classname[\'"]?\s*>([^\<]+)\<\/span\>/imu';
preg_match_all($regex, $data, $matches, PREG_PATTERN_ORDER);
print_r($matches[1]);

$regex = '/\<span\s*id=[\'"]?spanid[\'"]?\s*>([^\<]+)\<\/span\>/imu';
preg_match_all($regex, $data, $matches, PREG_PATTERN_ORDER);
print_r($matches[1]);

 


Creating read-only table in Oracle


A handy way to prevent any data modifications (update, delete, insert) in certain table in Oracle database is to create trigger that always fails:
 
create or replace no_dml
before insert or update or delete
on my_table
begin
    raise_application_error (-20000, 'my_table is read-only!');
end;

Using this approach you can select which tipe of data modifications to prevent. For example you can create table that allows only inserts without modifying existing data.

 

Find constraint details by its name in Oracle


When creating Oracle database schema it's useful to put names on all constraints to easily identify constraint violations later. But what happens if a constraint doesn't have name preset? Basicly you get a similar error:
 
ORA-00001: unique constraint (MYUSER.SYS_C00009843) violated
 
Now to identify which is the constraint and on which columns, run the following query:
 
select cons.table_name, cons.constraint_name, cons.constraint_type,
    cols.table_name, cols.column_name, cols.position
from user_constraints cons 
    inner join user_cons_columns cols 
        on cons.owner = cols.owner and cons.constraint_name = cols.constraint_name 
where cons.constraint_name = 'SYS_C00009843'


Recursively rename all files in a directory to another extension using bash


The following script renames all files in the current folder and its subfolders with extension .jad to .java

for file in `find . -type f -name *.jad`
do
    mv $file `dirname $file`/`basename $file .jad`.java
done


Find files not owned by user

Here's how to find files in user foo home folder that are not owned by foo

find /home/foo ! -user foo


Ignoring whitespace in subversion command line diff


Unlike diff subversion's svn diff doesn't have many modifier flags, particularly ignoring whitespace. To achieve this force svn to use desired external diff command using --diff-cmd option:

svn diff --diff-cmd diff -u -w working/copy/file


Adding all new files from the subversion working copy in a single command


When there are a lot of new files in the working copy, its not very handful to list them all in the svn add command. Here's how it happens automatically:

svn stat /local/working/copy | grep "^?" | awk -F "      " '{print $2}' | xargs svn add


1 2 3 4 5 6