简体   繁体   English

php regexp“AND”

[英]php regexp “AND”

I have to create translations for the project I work on. 我必须为我工作的项目创建翻译。 The simplest solution was to change all stings to a functioncall(string) so that I could get unique string hashes everytime. 最简单的解决方案是将所有stings更改为functioncall(string)以便每次都能获得唯一的字符串哈希值。

My code has the following different t() function uses: 我的代码有以下不同的t()函数用途:

<label for="anon"><?php echo t('anonymously? (20% extra)')?></label>

exit(t("Success! You made {amount} oranges out of it!", array('amount' => $oranges)));

echo t('You failed.');

My current regexp is: 我目前的正则表达式是:

$transMatches = preg_match_all('/[^a-z]t\([^)(]+/m', $contents, $matches);

The problem is that it fails on #1 example, matchin "anonymously?". 问题是它在#1例子中失败,匹配“匿名?”。 What I really want to achieve is: "match t( then match either ' or " then match anything except what you matched for ' or " and )" 我真正想要达到的目标是:“匹配t(然后匹配'或”然后匹配除了匹配'或'之外的任何内容和“)

Idea: t\\(['|"](.*?)[^'|"]\\)? 想法: t\\(['|"](.*?)[^'|"]\\)?

I cannot make above regexp to work. 我无法使上面的正则表达式工作。

How could I do AND in regexp so that it matches "['|"] AND )" OR "['|"] AND, array" 我怎么能在regexp中做AND,以便匹配“['|”] AND)“OR”['|“] AND,array”

Please help me on regexp and explain why it works. 请帮我解决正则表达式并解释它的工作原理。

Thank you! 谢谢!

Parsing function arguments may be quite complex , but you need to parse only the first argument which (for simplicity) can assume always to be string escaped either with ' or with " , thus those regexps may match" 解析函数的参数可以是相当复杂的 ,但你需要分析仅(为简单起见)可以总是假定是字符串逃过或者与第一个参数'"因此这些正则表达式可以匹配’

"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"
\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*'

Therefore you just need to match: 因此你只需要匹配:

'~[^\w\d]t\(\s*("[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\')~i'

[^\\w\\d] assumes that no test1t will match, \\s* makes you space tolerant... [^\\w\\d]假设没有test1t匹配, \\s*让你容忍空间......

With this regexp you'll get results like: 有了这个正则表达式,你将获得如下结果:

'anonymously? (20% extra)'
"Success! You made {amount} oranges out of it!"

And I can't imagine situation where you would need to parse out array too, can you describe it in comment/question? 我无法想象你需要解析数组的情况,你能在评论/问题中描述它吗?

Is this what you need? 这是你需要的吗?

Using Backreferences in The Regular Expression - http://www.regular-expressions.info/brackets.html 在正则表达式中使用反向引用 - http://www.regular-expressions.info/brackets.html

But it looks strange what are you doing, and why? 但看起来很奇怪你在做什么,为什么? Are you replacing the function call with some result? 你是否用一些结果替换了函数调用? why dont you just let it call the function and return translation from it? 为什么不让它调用函数并从中返回翻译?

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

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