There is an array of strings:

$test = array( 'Required route parameter not passed: :param', 'The :property property does not exist in the :class class', 'The requested view :file.:ext could not be found', 'PHP internal call', 'API method :uri: :code'); 

It is necessary to find and zamenit constructions of the form ': label' on ':'. Str_rot13 ('label'), there may be several constructions in a line, there is no nesting. The label starts with a colon, and ends with a space, a period, a comma, a colon.

The problem is that I do not know how to make a "regular schedule" for isolating these labels. I will also be grateful for intelligible materials on them, preferably with a large number of examples.

The final option:

 $test = preg_replace_callback('~:(\w+)~', function($matches){return ':'.str_rot13($matches[1]);}, $test); 

    2 answers 2

    You can do this using preg_replace_callback()

     <?php $test = array( 'Required route parameter not passed: :param', 'The :property property does not exist in the :class class', 'The requested view :file.:ext could not be found', 'PHP internal call', 'API method :uri: :code'); function str13_run($matches) { return ":".str_rot13($matches[1]); // первая группа } $test=preg_replace_callback('/:(\w+)/s','str13_run',$test); print_r($test); 

    Demo

    • Beautiful decision, but the result is a strange one. - Denis Khvorostin
    • what is strange? in the sense of: not at the beginning, I probably forgot it, or somewhere along the way I decided that it was not necessary. - zb '
    • symbol is devoured: in front of the label and for beauty, replace closure with f-tion - Anton Shamanov
    • Well, the symbol returned, and as for the beauty of closure, in the midst of the perl function, I would argue, it’s not js, it’s not pretty here. especially in php closure work very peculiar. - zb '
    • But not. Hang up All OK. - Denis Khvorostin
     // Исходный массив $test = array( 'Required route parameter not passed: :param', 'The :property property does not exist in the :class class', 'The requested view :file.:ext could not be found', 'PHP internal call', 'API method :uri: :code'); // Результирующий массив $result = array(); // Проходим по всем элементам массива foreach($test as $i) { // Находим все нужные нам элементы и складываем их в массив $out preg_match_all('/\:([^ \.\:]+)/i', $i, $out); // Обновляем блоки замен по требуемой маске. Теперь в массиве $out // два подмассива: $out[0] - элементы, которые надо заменить // $out[1] - соответствующие им элементы-заместители foreach($out[1] as &$ii) { $ii = ':' . str_rot13($ii); } // Добавляем в результирующий массив результаты замены $out[0] на $out[1] $result[] = str_replace($out[0], $out[1], $i); } 

    The best of regular expressions: J. Friedl’s book Regular Expressions . Mastkhev.

    • one
      thanks, the only thing that was necessary was to call the str_rot13 function, so the eicto option is more kosher) - Anton Shaman
    • one
      Yes. @Eicto has a super version. - Denis Khvorostin