function item_redir_and_replace_images($body, $images, $cid) { $origbody = $body; $newbody = ''; $cnt = 1; $pos = get_bb_tag_pos($origbody, 'url', 1); while ($pos !== false && $cnt < 1000) { $search = '/\\[url\\=(.*?)\\]\\[!#saved_image([0-9]*)#!\\]\\[\\/url\\]' . '/is'; $replace = '[url=' . z_path() . '/redir/' . $cid . '?f=1&url=' . '$1' . '][!#saved_image' . '$2' . '#!][/url]'; $newbody .= substr($origbody, 0, $pos['start']['open']); $subject = substr($origbody, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']); $origbody = substr($origbody, $pos['end']['close']); if ($origbody === false) { $origbody = ''; } $subject = preg_replace($search, $replace, $subject); $newbody .= $subject; $cnt++; $pos = get_bb_tag_pos($origbody, 'url', 1); } $newbody .= $origbody; $cnt = 0; foreach ($images as $image) { // We're depending on the property of 'foreach' (specified on the PHP website) that // it loops over the array starting from the first element and going sequentially // to the last element $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody); $cnt++; } return $newbody; }
function bb_tag_preg_replace($pattern, $replace, $name, $s) { $string = $s; $occurance = 1; $pos = get_bb_tag_pos($string, $name, $occurance); while ($pos !== false && $occurance < 1000) { $start = substr($string, 0, $pos['start']['open']); $subject = substr($string, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']); $end = substr($string, $pos['end']['close']); if ($end === false) { $end = ''; } $subject = preg_replace($pattern, $replace, $subject); $string = $start . $subject . $end; $occurance++; $pos = get_bb_tag_pos($string, $name, $occurance); } return $string; }