PHP preg_replace - 在结果中包含模式/完整主题

时间:2014-01-22 16:46:28

标签: php regex preg-replace

我遇到了一个无法解决的问题:替换......

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

应该成为:

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

我尝试了一个简单的替换:

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

但结果看起来像

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

当然系统已经被替换了两次。所以我决定使用 preg_replace

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

但现在几乎所有东西都丢失了,因为只返回括号中的单词而我不知道如何包含该模式或返回替换的$ subject。 $ pattern改变了,所以我不能写“locale(...”到$ replacement /我不知何故必须返回一个替换模式......

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

你能帮我找到正确的结果吗?

4 个答案:

答案 0 :(得分:2)

可能只需要替换第二个变量 每次要更换某些东西时都要做一个新的preg_replace 此正则表达式使用分支重置来解析引号。

 # 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)
 )

答案 1 :(得分:0)

我猜你正在寻找这样的东西:

$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;
}

答案 2 :(得分:0)

这实际上取决于这个输入的形成情况,但是这样的内容对你的例子有用:

(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")

答案 3 :(得分:0)

更换需要多复杂?这将在该特定示例中起作用:

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

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

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