Skip to main content

Posts

Showing posts from January, 2011

PHP : Regular Expression

Adding Comments preg_match("/^ (1[-\s.])? # optional '1-', '1.' or '1' ( \( )? # optional opening parenthesis \d{3} # the area code (?(2) \) ) # if there was opening parenthesis, close it [-\s.]? # followed by '-' or '.' or space \d{3} # first 3 digits [-\s.]? # followed by '-' or '.' or space \d{4} # last 4 digits $/x",$number); Let’s put it within a code segment. $numbers = array( "123 555 6789", "1-(123)-555-6789", The trick is to use the ‘x’ modifier at the end of the regular expression . It causes the whitespaces in the pattern to be ignored, unless they are escaped (\s). This makes it easy to add comments. Comments start with ‘#’ and end at a newline. Greedy vs. Ungreedy Before I start explaining this concept, I would like to show an example first. Let’s say we are looking to find anchor tags in an html text: $html = 'Hello World! '; if (preg_...

simple recursive function to copy entire directories

<?php function recurse_copy ( $src , $dst ) {     $dir = opendir ( $src );     @ mkdir ( $dst );     while( false !== ( $file = readdir ( $dir )) ) {         if (( $file != '.' ) && ( $file != '..' )) {             if ( is_dir ( $src . '/' . $file ) ) {                 recurse_copy ( $src . '/' . $file , $dst . '/' . $file );             }             else {                 copy ( $src . '/' . $file , $dst . '/' . $file );             }         }     }     closedir ( $dir ); } ?>

CREATE JAR FILE

1) Create file HelloWorld.java 2) Compile HelloWorld.java (eg : javac Helloworld.java) to HelloWorld.class 3) create a file named manifest.txt 4) Add content "Main-Class: HelloWorld" to manifest.txt file 5) Now run this command on command Prompt ( E:\java\exe>jar -cfm test.jar manifest.txt HelloWorld.class) 6) and then command java -jar test.jar or double click test.jar file

preg_replace_callback inside a class

<?php class MyClass {   function preg_callback_url ( $matches )   {     //var_dump($matches);     $url = $matches [ 1 ]. $matches [ 2 ];     $text = '' ;     $pos = strpos ( $url , ' ' );     if ( $pos !== FALSE ) {       $text = trim ( substr ( $url , $pos + 1 ));       $url = substr ( $url , 0 , $pos );     }     return '<a href="' . $url . '" rel="nofollow">' .(( $text != '' ) ? $text : $url ). '</a>' ;   }   function ParseText ( $text )   {     return preg_replace_callback ( '/\[(http|https|ftp)(.*?)\]/iS' ,array( & $this , 'preg_callback_url' ), $text );   } } ?>