简体   繁体   English

如何用PHP替换同一行或不同行的多个字符串?

[英]How can replace multiple strings on same line or not in same line with PHP?

Need regex to replace all inside brackets with myReplace !important need keep spaces and lines as is! 需要用正则表达式将所有内部括号替换为myReplace!重要的是需要保持空格和线条不变!

$fileContent = <div class="panel-body">
    {toChangeOne}{toChangeTwo}
                            {  
                    toChangeTree
                    }
</div>

$change = preg_replace('NEEDED_REGEX', 'myReplace', $fileContent);

Okay, so basically all you'd need to do is look for a set of curly braces and replace that, plus everything inside of it. 好的,基本上,您所需要做的就是寻找一组花括号并将其替换,以及其中的所有内容。

Something like this should work for you: 这样的事情应该为您工作:

<?php

$fileContent = '<div class="panel-body">
    {toChangeOne}{toChangeTwo}
                            {  
                    toChangeTree
                    }
</div>';

$fileContent = preg_replace('~\{.*?\}~sm', 'myReplace', $fileContent);

print $fileContent;    

Here's what that expression means \\{.*?\\} : 该表达式的含义是\\{.*?\\}

  • \\{ - Looking for an opening curly brace { . \\{ -寻找左大括号{ We need to escape it with a backslash \\ since curly braces have special meaning in regex. 我们需要使用反斜杠\\对其进行转义,因为花括号在正则表达式中具有特殊含义。
  • .*? - Match any character . -匹配任何字符. , any number of times * until we hit the next part of our statement ? 多少次*直到我们执行语句的下一部分? .
  • \\} - The next part of our statement is a closing curly brace } . \\} -我们语句的下一部分是右花括号} Again, we need to escape it with a backslash \\ . 同样,我们需要使用反斜杠\\对其进行转义。

Here is a working demo: 这是一个工作示例:

http://ideone.com/Pi8OvI http://ideone.com/Pi8OvI

You could also approach your problem using an array of keys to change as below. 您还可以使用一系列键来更改您的问题,如下所示。 This might be helpful when trying to replace multiple strings. 尝试替换多个字符串时,这可能会有所帮助。

<?php

// array with keys that you'll be changing in your text
$toChange = array(
"{toChangeOne}" => "First Change",
"{toChangeTwo}" => "Second Change",
"{toChangeThree}" => "Third Change"
);


$fileContent = '<div class="panel-body">
    {toChangeOne}{toChangeTwo}
                            { 
                    toChangeThree
                    }
</div>';

// loop through all the keys you want to change
foreach($toChange as $key => $value){

    // prep regex
    // remove the openning and curly braces this 
    // way we can match anything that matches our 
    // keys even if there's a mixture of returns 
    // or empty spaces within the curly braces 
    $key_text = str_replace("{", "", $key);
    $key_text = str_replace("}", "", $key_text);

    // "\{"             - matches the character "{" literally
    // "(\s|\s+)?"      - matches any white space. In our case 
    //                    we might want it to be optional hense 
    //                    the "?"
    // "\}"             - matches the character "}" literally   
    $regex = '/\{(\s|\s+)?'.$key_text.'(\s|\s+)?\}/';

    $fileContent = preg_replace($regex, $value, $fileContent);
}

echo $fileContent;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM