Regex

Here is a dump of my favourite regex samples.

<?php  
// Replace Esperanto X notation user input with accented characters.  
$value = 'cxaro auxto sxati igxi'; // A string to test  
$patterns  = array(   '/sx/','/cx/','/gx/','/jx/','/ux/','/hx/','/Sx/','/Cx/','/Gx/','/Jx/','/Ux/','/Hx/');  
$replacements = array( 'ŝ',   'ĉ',   'ĝ',   'ĵ',   'ŭ',   'ĥ',   'Ŝ',   'Ĉ',   'Ĝ',   'Ĵ',   'Ŭ',   'Ĥ');  
$value = preg_replace($patterns, $replacements, $value);

echo $value;  
?>

<?php  
// Scrape movie quotes from a web site.  
$url = 'http://www.rottentomatoes.com/m/gone_with_the_wind/quotes/';  
$regex = '/<span class="line">(.+?)<\/span>/';  
$data = file_get_contents($url);

preg_match_all($regex,$data,$match);  
//print_r($match);  
// The array is in 2 parts. We just need the second part, $match[1]  
$quotes = array_rand($match[1],1);  
$quote = $match[1][$quotes];  
?>

 

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*