-1

I try to find a way to add number to a pattern inside a XML.

String inside XML looks like :

Blabla blabla ${bâtiment} : blabla

And I want at the end

Blabla blabla ${bâtiment1} : blabla

I found something like this :

preg_replace('/\$\{(\w)\w+\}/', '\${\\1#' . $i . '}', $cloneXML);

But it doesn't work, the result contain only the first letter :

Blabla blabla ${b1} : blabla

I don't find something to get all the word :/

Thank for your help

Furya
  • 299
  • 2
  • 6
  • 15

1 Answers1

0

Use the PCRE modifier /u for unicode and put brackets around the reference in the replacement, so that it doesn't get concatenated with the number from your variable $i. Also your regular expression did not catch whole words, so maybe this is what you're looking for:

<?php

$cloneXML = 'Blabla blabla ${bâtiment} : blabla';
$i = 42;

print preg_replace('/\$\{(\w*)\}/u', '{${1}' . $i . '}', $cloneXML);
// => Blabla blabla {bâtiment42} : blabla

See output: 3v4l.org/k8mIQ

Sven Mich
  • 227
  • 2
  • 9