Skip to content

Instantly share code, notes, and snippets.

@lomadurov
Created July 30, 2014 12:40
Show Gist options
  • Save lomadurov/51fc5e199ddc2fbf0792 to your computer and use it in GitHub Desktop.
Save lomadurov/51fc5e199ddc2fbf0792 to your computer and use it in GitHub Desktop.
<?
// func.global callback
function getImagePath ($matches) {
global $an;
return $an[$matches[1]];
}
// func.global
function optimize($text, $img_domain, $an) {
// ...
$patt = '/\[img name\=\"([A-Za-z0-9._-]+)\"\]/';
// ...
$GLOBALS['an'] = &$an;
// ...
$result = preg_replace_callback($patt, 'getImagePath', $text);
echo $result;
}
// ajax editcom
function ajax_editcom() {
$url = "p.local"; // $c['title_url'];
// ...
$comment = '[img name="14066778417910.gif"] [img name="14066778411187.png"]';
// get mysql comment by id
$oldComment = '<div class="b-p b-p_type_image"><a href="http://p.local/images/big_size_comm/2014-07_6/14066778417910.gif"><img class="b-image" src="http://s4.p.local/images/previews_comm/2014-07_6/14066778417910.gif"></a></div><div class="b-p b-p_type_image"><a href="http://p.local/images/big_size_comm/2014-07_6/14066778411187.png"><img class="b-image" src="http://s4.p.local/images/previews_comm/2014-07_6/14066778411187.png"></a></div>';
// ...
$an = false;
if (preg_match_all('/src="http:\/\/(s[1-9]\.'.$url.')([^"]*)"/Us', $oldComment, $matches)) {
$an = array();
$img_domain = $matches[1][0];
foreach ($matches[2] as $path) {
$an[basename($path)] = $path;
}
}
// ...
optimize($comment, $img_domain, &$an);
}
ajax_editcom();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment