In PHP, if I replace "a" in the string "ababab c", how do I replace it with a matching index (i.e., "1 b 2 b 3 b c")?
$str = 'a b a ba a'; $count = 1; while(($letter_pos = strpos($str, 'a')) !== false) { $str = substr_replace($str, $count++, $letter_pos, 1); }
use preg_replace_callbackinstead.
preg_replace_callback
PHP 5.3.0 example (not verified):
$i = 0; preg_replace_callback("/a/", function( $match ) { global $i; return ++$i; }, "a b a b a b c");
, preg _ *?
:
$numerals = range(1, 10); $str = str_replace('a', $numerals, $str);
str_replace() . , , .
Source: https://habr.com/ru/post/1781417/More articles:Как захватить экран iPhone с анимацией UIImageView? - objective-cMongoDB: how to execute a query on the result of another query (nested queries)? - mongodbHow to add an extra button to the Extjs grid header menu - extjsGWT java URL Validator - javaDeploying a browser using Dojo? - javascriptdebugger for C / C ++ and Java - javaUnit testing cookies in PHP - httpWPF management inside ElementHost is invisible - visibilityIs it possible to write Win32.dll files using C # .NET? - c #Why does SQL Management Studio add cross-connect? - sqlAll Articles