PHP preg_replace - include template / full object in result

I have a problem that I could not solve on my own: replace ...

locale("Sendx", "Send")
locale("System", "System")

should become:

locale("Sendx", "Subsub")
locale("System", "Newsys")

I tried a simple replacement:

$mysearchword = "System"; #changes in a loop
$myreplaceword = "Newsys"; #also changes in the loop
$oneline = str_replace($mysearchword, $myreplaceword, $oneline);

but the result looks like

locale("Sendx", "53ND")
locale("Newsys", "Newsys") #problem with the doubled word

Of course, the system was replaced by both times. So I decided to use preg_replace

$pattern = '/locale\\(["|\']([^"\']*)["|\'], ["|\']([^"\']*)["|\']\\)/';
$replacement = '${1}, Newsys';
$subject = 'locale("System", "System")';
echo preg_replace($pattern, $replacement, $subject, -1 );

But now almost nothing is missing, because only the words in brackets are returned, and I have no idea how to include the template or return the replaced $ subject object. The $ template was changed, so I could not write "locale (..." in $ replacement / I, somehow I should return the replaced template ...

System, Newsys # No idea how to combine $replacement with $pattern...

Could you help me get the right result?

+4
source share
4 answers

, - :

$input  = 'locale("Sendx", "Send")';
$output = preg_replace('/, "(.*?)"/', ', "Subsub"', $input);
echo $output;
echo "\n";

$input  = 'locale("System", "System")';
$output = preg_replace('/, "(.*?)"/', ', "Newsys"', $input);
echo $output;

:

locale("Sendx", "Subsub")
locale("System", "Newsys")

/, "(.*?)"/ " , "NEW_WORD"

, :

$input = array(
  'locale("Sendx", "Send")' => 'Subsub',  
  'locale("System", "System")' => 'Newsys'
);

foreach($input as $string => $replacement) {
    $output = preg_replace('/, "(.*?)"/', ', "' . $replacement . '"', $string);
    echo $output. PHP_EOL;
}
+1

.
preg_replace , - .
reset .

 # FIND: 
 # $pattern =
 # '/(?s)(?|(locale\s*\(\s*"[^"\\\]*(?:\\\.[^"\\\]*)*"\s*,\s*"\s*)'
 # . $whatyouwanttofind .
 # '(\s*"\s*\))|(locale\s*\(\s*\'[^\'\\\]*(?:\\\.[^\'\\\]*)*\'\s*,\s*\'\s*)'
 # . $whatyouwanttofind .
 # '(\s*\'\s*\)))/';
 # 
 # REPLACE:  ${1}$whatyouwanttoreplace${2}

 (?s)
 (?|
      (                             # (1 start)
           locale
           \s* 
           \(
           \s* 
           " [^"\\]* (?: \\ . [^"\\]* )* " 
           \s* , \s* 
           " 
           \s* 
      )                             # (1 end)
      what you want to find
      (                             # (2 start)
           \s* 
           " 
           \s* 
           \)
      )                             # (2 end)
   |  
      (                             # (1 start)
           locale
           \s* 
           \(
           \s* 
           ' [^'\\]* (?: \\ . [^'\\]* )* ' 
           \s* , \s* 
           ' 
           \s* 
      )                             # (1 end)
      what you want to find
      (                             # (2 start)
           \s* 
           ' 
           \s* 
           \)
      )                             # (2 end)
 )
+2

, , - :

(locale\("[^"]*",\s*")System("\))

RegExr

PHP:

$find = 'System';
$replace = 'Newsys';

$pattern = '/(locale\("[^"]*",\s*")'.$find.'("\))/';
$replacement = '$1'.$replace.'$2';
$subject = 'locale("System", "System")';
echo preg_replace($pattern, $replacement, $subject); 
//Outputs: locale("System", "Newsys")
0

? :

$str = '
locale("Sendx", "Send")
locale("System", "System")';

$str =
str_replace('"System", "System"', '"System", "Newsys"',
str_replace('"Send"', '"Subsub"', $str));

echo "<pre>$str</pre>";
0

All Articles