Over a million developers have joined DZone.

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  tag emulating being
// passed thru a proxy.


$html = file_get_contents('http://www.yahoo.com/');
print "$html

"; $attr= 'src'; $webroot='proxy'; $html=preg_replace('/(\s)?'.$attr.'="([^\s]*?)"/ei', "make_new_img_tag('$attr','$2','$1','$webroot');", $html); print ""; function make_new_img_tag($attr, $filename, $prefix, $webroot) { $b64val = base64_encode($filename); return $prefix$attr.'="'.$webroot.'/browse/'.$b64val.'"'; }
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}