3v4l.org

run code in 300+ PHP versions simultaneously
<?php $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 for 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
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** ---
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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** ---
Output for 7.3.0 - 7.3.33
Warning: preg_replace_callback() expects at most 5 parameters, 6 given in /in/7mDqd on line 29 --- Warning: preg_replace_callback() expects at most 5 parameters, 6 given in /in/7mDqd on line 29 --- Warning: preg_replace_callback() expects at most 5 parameters, 6 given in /in/7mDqd on line 29 --- Warning: preg_replace_callback() expects at most 5 parameters, 6 given in /in/7mDqd on line 29 --- Warning: preg_replace_callback() expects at most 5 parameters, 6 given in /in/7mDqd on line 29 ---

preferences:
167.42 ms | 402 KiB | 156 Q