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

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

preg_replace technique #1

// This is a useful technique for passing matched data into other
// functions to be evaluated and replaced in parsed strings.
//
// This example is simple. It rewrites the <img> tag emulating being
// passed thru a proxy.

   1  
   2  $html = file_get_contents('http://www.yahoo.com/');
   3  print "$html<br><br>";
   4  $attr= 'src';
   5  $webroot='proxy';
   6  $html=preg_replace('/(\s)?'.$attr.'="([^\s]*?)"/ei',
   7                     "make_new_img_tag('$attr','$2','$1','$webroot');",
   8                     $html);
   9  print "<!-- $html -->";
  10  
  11  
  12  function make_new_img_tag($attr, $filename, $prefix, $webroot) {
  13      $b64val = base64_encode($filename);
  14      return $prefix$attr.'="'.$webroot.'/browse/'.$b64val.'"';
  15  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS