Preg_match_all with callback?

I am interested in replacing real-time numerical matches and manipulating them with hexadecimal.

I was wondering if this is possible without using a foreach loop.

so that...

every thing in between:

= {numerical value} ;

will be manupulated to:

= {hexadecimal numeric value} ;

preg_match_all('/\=[0-9]\;/',$src,$matches);

Is there any answer to preg_match_all, so instead of preforming the loop after that I can manipulate them as soon as preg_match_all catches every match (in real time).

this is not the correct syntax, but simply you can understand:

preg_match_all_callback('/\=[0-9]\;/',$src,$matches,{convertAll[0-9]ToHexadecimal});
+4
source share
2 answers

You want to preg_replace_callback().

/=\d+?;/, ...

function($matches) { return dechex($matches[1]); }

, ...

preg_replace_callback('/=(\d+?);/', function($matches) { 
   return dechex($matches[1]);
}, $str);

CodePad.

lookbehind/forward , 'dechex' .

+6

T-Regx, ! ( , , API)

pattern('=(\d+?);')->replace($str)->group(1)->callback('dechex');

pattern('=(\d+?);')->replace($str)->group(1)->callback(function (Group $group) {
    return dechex($group);
});
0

Source: https://habr.com/ru/post/1570080/


All Articles