Is there a way to break the search for preg_replace_callback
when it reaches a certain index?
For example:
$content = preg_replace_callback('/'.preg_quote($_POST['query'],'#').'/i', function($matches) use($replacements,&$index) {
if ( $index > count($replacements) ) {
// I tried break; but doesn't work
}
if ( $replacements[$index] != '' ) {
$matches[0] = $replacements[$index];
}
$index++;
return $matches[0];
}, $content);
You can just use the elseif to ignore the second if
$content = preg_replace_callback('/'.preg_quote($_POST['query'],'#').'/i', function($matches) use($replacements,&$index) {
if ( $index > count($replacements) ) {
// I tried break; but doesn't work
} elseif ( $replacements[$index] != '' ) {
$matches[0] = $replacements[$index];
}
$index++;
return $matches[0];
}, $content);
edit
preg_replace_callback ( mixed pattern, callback callback, mixed subject [, int limit [, int &count]] )
limit The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).
count If specified, this variable will be filled with the number of replacements done.