I don't quite have complete clarity about what is optional and what is required in your article placeholders, but this whole task can be contained within a single preg_replace_callback()
call.
The pattern that I have build will require the first argument (catid
) in the placeholder; the additional two arguments will be optional, and the second argument (charset
) is omittable when only the third argument (number
) is given.
The pattern requires the first argument to be an integer, the second argument must be letters only, and the third argument must be an integer. If your requirements differ from these rules, it will be trivial to adjust.
Code: (Demo with 5 different sample article texts)
$articles = [
(object)['text' => 'Some text {reillorama:5/dots/1} some text {reillorama:12/foo}'],
(object)['text' => 'Only text with no placeholders'],
(object)['text' => 'Some text {reillorama:33} some text {reillorama:1/bar} some text'],
(object)['text' => 'Some text {reillorama:5/word} some text {reillorama:2} some text {reillorama:555555/noSpacesInWord/7777777}'],
(object)['text' => 'The pattern also allows the middle parameter to be omitted {reillorama:6/7}'],
];
$firstLookup = [5 => 'five', 12 => 'twelve', 1 => 'one', 2 => 'two'];
$secondLookup = ['foo' => 'fighters', 'bar' => 'none', 'word' => 'up', 'noSpacesInWord' => 'replacement'];
$thirdLookup = [1 => 'another one', 7 => 'seven'];
foreach ($articles as $article) {
$article->text = preg_replace_callback(
'~{reillorama:(\d+)(?:/([a-z]+))?(?:/(\d+))?}~i',
function($m) use ($firstLookup, $secondLookup, $thirdLookup) {
return '**'
. ($firstLookup[$m[1]] ?? '[first not found]')
. ($m[2] === null ? '' : (isset($secondLookup[$m[2]]) ? $secondLookup[$m[2]] : '[second not found]'))
. ($m[3] === null ? '' : (isset($thirdLookup[$m[3]]) ? $thirdLookup[$m[3]] : '[third not found]'))
. '**';
//var_export($m); if you want to see what is in the matches array
},
$article->text,
-1,
$count,
PREG_UNMATCHED_AS_NULL
);
echo "{$article->text}\n---\n";
}
Output:
Some text **five[second not found]another one** some text **twelvefighters**
---
Only text with no placeholders
---
Some text **[first not found]** some text **onenone** some text
---
Some text **fiveup** some text **two** some text **[first not found]replacement[third not found]**
---
The pattern also allows the middle parameter to be omitted **[first not found]seven**
---
Of course, when you implement this snippet in your application, you won't need the foreach()
and you can simply return preg_replace_callback(...
instead of overwriting the $article-text
variable.
If you need a hand with tailoring this solution to your actual needs, I can assist with that.
I suppose your custom function within preg_replace_callback()
might look a little like this:
function($m) use ($path) {
$image = new images();
$image->categoryid = $m[1];
$allimages = $image->getImages();
// I don't have any clue what you'd like to do with $m[2] and $m[3].
ob_start();
include $path;
return ob_get_clean();
}
str_replace()
calls to escape characters in your$regex_cat
pattern -- this whatpreg_quote()
is for. Your question requirements are not clear to me, so I cannot offer a refined solution for your issue. This SO page seems like a worthwhile read: stackoverflow.com/q/17792817/2943403