简体   繁体   English

php - 正则表达式用大括号和引号替换字符串

[英]php - regex replacing strings with braces and quotes

I see a lot of questions here regarding regex in general , but the problem is that they are usually (like mine) quite localized, and difficult to deduct if one is not a regex expert .. 我在这里看到很多关于正则表达式的问题,但问题是它们通常(像我的)非常本地化,如果一个人不是正则表达式专家,很难扣除。

My string involves characters like quotes and braces , which are quite known for making regex more difficult. 我的字符串包含引号和大括号等字符,这些字符因使正则表达式更难而闻名。

I would like to know the expression strings (search, replace) I need in order to perform this task. 我想知道我需要的表达式字符串(搜索,替换)来执行此任务。

in other words , in : 换句话说,在:

 ereg_replace (string pattern, string replacement, string subject) 

I will need the string pattern and string replacement expressions. 我需要string patternstring replacement表达式。

my string is 我的字符串是

array('val' => 'something', 'label' => 'someword'),

I need to change the last part : 我需要改变最后一部分:

'label' => 'someword'),

to

'label' => __('someword','anotherstring')),

I will be using php for that , but I would also like to test it ( and also use in other cases) with Notepad ++ . 我将使用php,但我也想用Notepad ++测试它(并在其他情况下使用)。 ( I do not know if it actually changes something regarding the search and replace strings ). (我不知道它是否真的改变了搜索和替换字符串的内容)。

Note that the string someword can also be SomeWord or SOMEWORD or even Some word or Some_Word on cases, meaning it can contain spaces, underscores or actually almost any character from within...) 请注意,字符串someword也可以是SomeWordSOMEWORD甚至是Some wordSome_Word的情况,这意味着它可以包含空格,下划线或实际上几乎任何字符......)

Edit I : forgot to mantion that the __() part is of course wordpress textdomain function for translations . 编辑I:忘了提及__()部分当然是wordpress textdomain函数用于翻译。 eg __('string','texdomain') 例如__('string','texdomain')

Edit II : 编辑二:

I am sorry if I come off too exigent or demanding in the comments , I really do try to UNDERSTAND and not only copy-paste a solution that might not work for me in other cases .. :-) 我很抱歉,如果我在评论中过于紧迫或要求,我真的会尝试理解而不仅仅是复制粘贴一个在其他情况下可能对我不起作用的解决方案.. :-)

Edit III : 编辑III:

By the help of THIS tool , I understood that my basic misunderstanding is the possibility to use VARIABLES inside regex . 这个工具的帮助下,我明白我的基本误解是在正则表达式中使用VARIABLES的可能性。 The $1 is actually all I needed for better understanding . $1实际上是我需要的,以便更好地理解。

the (incredibly simple) pattern that will work also in notepad++ (令人难以置信的简单)模式,也可用于记事本++

Pattern: 'label' => ('.*')

Replace: 'label' => __(\1,'textdomain')

(In notepad++ it is called Tag Region (not var) and it is marked as \\1 (在记事本++中,它被称为标记区域(不是var),它被标记为\\1

If you will always be looking for the label key, you should be able to do something like this: 如果您一直在寻找label密钥,您应该能够做到这样的事情:

$pattern = "/array\((.*), 'label' => '(.*)'/U";
$added_string = 'anotherstring';
$replacement = 'array($1, ' . "'label' => __('" . '$2' . "','$added_string'";
$final_string = preg_replace($pattern, $replacement, $original_string);

For given inputs and outputs in question pattern: 'label' => ('.*') is enough to match strings and perform replacement. 对于给定输入和输出的问题模式: 'label' => ('.*')足以匹配字符串并执行替换。 This pattern matches following part from a string: 'label' => any character between ' . 此模式匹配字符串后面的部分:'label'=> '之间'任何字符 Part of pattern in braces will group any character between ' that can be later accessed with $1 . 大括号中的一部分模式将组合'之后可以使用$1访问的任何字符 Eg: 例如:

$str = "array('val' => 'something', 'label' => 'some testing string_with\$specialchars\/'),";
$str = preg_replace('/\'label\' => (\'.*\')/', '\'label\' => __($1, \'some other string\')', $str);
echo $str;
//Outputs:
//   array('val' => 'something', 'label' => __('some testing string_with$specialchars\/', 'some other string')),
<?php
$strings = array('some word', 'some Word', 'SOMEword', 'SOmE_Word', 'sOmE_ WOrd');
$pattern = '/([a-z]+)([^a-z]*)([a-z]+)/i';
foreach($strings as $v){
 echo preg_replace($pattern, 'otherword', $v)."<br>";
}
?> 

output: 输出:

otherword
otherword
otherword
otherword
otherword

EDIT: 编辑:

$pattern = "/('label'\s=>\s')(([a-z]+)([^a-z]*)([a-z]+))('\),)/i";
$otherword = 'otherword';
$replacement = "'label' => __('$2','$otherword')),";
echo preg_replace($pattern, $replacement, "'label' => 'someword'),");

output: 输出:

'label' => __('someword','otherword')), 'label'=> __('someword','otherword')),

DEMO DEMO

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

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