PHP 简单正则表达式

发布于 2024-10-08 05:26:54 字数 1432 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

标点 2024-10-15 05:26:54

虽然我不是 100% 确定你在问什么,但我认为这会满足你的要求:

preg_replace(
  '/
    \'\h*\.\h*                         # Match a quote and a dot
    (\$(?!(?:help)?txt\b)              # Avoid matching $txt or $helptxt...
       \w+)(?:(\[)([\'"])(\w+)\3(\]))? # ...but do match $var or $arr["idx"]
    \h*\.\h*\'                         # Match a quote and a dot
   /x', 
  '{$1$2$4$5}',
  $string)

注释有帮助,但正则表达式仍然看起来像克苏鲁咒骂,所以让我们把它分成几部分。正则表达式由分隔符 / 包围,它们之所以存在,是因为 preg_replace 需要它们。最后的 /x 允许我们将空格和注释嵌入到正则表达式中,我们用它来使其更容易理解。出现在开头和结尾的 \'\h*\.\h* 咒语与 ' . 等匹配。 (如果要匹配单引号或双引号,请将 \' 替换为 [\'"]。)' 只需要用 \ 转义,因为我们在单引号字符串中 \h 表示任何水平空白(几乎是空格或制表符),而 则表示。 * 允许任意数字,那么 \. 是一个文字 .,而 \h* 则匹配更多的空格

。第二行,我们开始匹配一个变量,将其放入带有括号的捕获组 1 中。我们首先匹配 \$,一个文字美元符号,然后使用 (?!(? :help)?txt\b) 内部很简单:(?:help)? 可以选择匹配 help?: 防止它被分配给捕获组),然后我们匹配 txt,然后是 \b 强制断字(这样 $ txta 仍然匹配)。(?!...) 是负向前瞻:它不消耗任何输入,但仅在其内部不匹配时才匹配。 。因此,只有当变量名称不是 txthelptxt 时,我们才会在此处进行匹配。

在第三行,我们完成了变量的匹配。 \w+ 仅匹配一个或多个“单词字符”:字母数字和下划线。此后,我们将变量名称保存到捕获组 1 中 - 不包含数组索引(如果有的话)!然后我们匹配 (?:...)? 中的某些内容,它只是选择性地匹配它,而不将其保存到捕获组。内部与['array_access'] 位(如果存在)匹配。我们在这里保存了大量的捕获组,以便我们可以根据需要删除引号。首先,我们将文字 [\[ 匹配,并将其存储在捕获组 2 中。然后,我们将单引号或双引号与 [\ 匹配'"],并将其存储在捕获组 3 中。然后,我们匹配另一个单词字符字符串(即数组索引),并将其存储在捕获组 4 中。然后,我们通过匹配 < 来匹配我们开始的引号code>\3,它捕获组 3。最后,我们匹配一个右大括号并将其存储在捕获组 5 中。最后,我们再次匹配点和引号(如果您不想匹配 <。 code>$arr["idx"] 但只是 $arr['idx'],然后更改 ([\'"]) 和 < code>\3 更改为 \',并将替换字符串更改为 {$1$2$3$4}。)

然后我们将其替换为 '{ $1$2$4$5}'。如果您还记得,$1 是变量名称(和美元符号),$2[$4是数组索引(不带引号),$5]$2$4$5 要么全部被定义,要么没有被定义,所以这只会在我们需要的时候产生方括号。

在字符串上运行此命令将匹配并替换正则表达式的每个出现位置,我认为这应该完全符合您的要求。

Although I'm not 100% sure what you're asking, I think this will do what you want:

preg_replace(
  '/
    \'\h*\.\h*                         # Match a quote and a dot
    (\$(?!(?:help)?txt\b)              # Avoid matching $txt or $helptxt...
       \w+)(?:(\[)([\'"])(\w+)\3(\]))? # ...but do match $var or $arr["idx"]
    \h*\.\h*\'                         # Match a quote and a dot
   /x', 
  '{$1$2$4$5}',
  $string)

The comments help, but the regex still looks like Cthulhu swearing, so let's break it down into pieces. The regex is surrounded by the delimiters /, which are just there because preg_replace requires them. The /x at the end allows us to embed spaces and comments into the regex, which we use to make it more comprehensible. The \'\h*\.\h* incantation, which appears at the beginning and the end, matches ' . and the like. (If you want to match single or double quotes, replace the \' with [\'"].) The ' only needs to be escaped with \ because we're in a single-quoted string. The \h indicates any horizontal whitespace (pretty much a space or a tab), with the * allowing any number. Then the \. is a literal ., and the \h* matches more space.

On the second line, we start to match a variable, which we put in capturing group 1 with parentheses. We first match \$, a literal dollar sign, and then we use (?!(?:help)?txt\b). The interior is simple: (?:help)? optionally matches help (the ?: prevent it from being assigned to a capturing group), and then we match txt, and then there's \b which forces a word break (so that $txta is still matched). The (?!...) is a negative lookahead: it doesn't consume any input, but only matches if its interior doesn't. Thus, we only match here if the variable name isn't txt or helptxt.

On the third line, we finish matching the variable. The \w+ just matches one or more "word characters": alphanumerics and the underscore. After this, we save the variable name into capturing group 1—without the array index, if there is one! We then match something in (?:...)?, which just optionally matches it without saving it to a capturing group. The interior matches the ['array_access'] bit, if it exists. We save lots of capturing groups here so that we can delete the quotes like you want. First, we matches a literal [ with \[, and store this in capturing group 2. Then, we match either a single or a double quote with [\'"], and store this in capturing group 3. We then match another string of word characters, which is the array index, and store it in capturing group 4. We then match whichever quote we started with by matching \3, which is capturing group 3. Finally, we match a closing brace and store it in capturing group five. Finally, we match the dot and quote again. (If you don't want to match $arr["idx"] but just $arr['idx'], then change each of ([\'"]) and \3 to \', and change the replacement string to {$1$2$3$4}.)

We then replace this with '{$1$2$4$5}'. If you recall, $1 is the variable name (and the dollar sign), $2 is the [, $4 is the array index (without quotes), and $5 is the ]. Either all of $2, $4, and $5 are defined or none are, so this only produces square brackets when we want them.

Running this on a string will match and replace every occurrence of the regex, which should, I think, do exactly what you want.

烟花易冷人易散 2024-10-15 05:26:54

您可以通过两次传递来完成此操作,一次用于无括号样式,另一次用于括号样式。

对于括号,替换

'\s*\.\s*\$(?!txt|helptxt)(\w+)\['(\w+)'\]\s*\.\s*'  

{\$1[$2]}

然后对于不带括号的变量,替换

'\s*\.\s*\$(?!txt|helptxt)(\w+)\s*\.\s*' 

{\$1}

您需要确保在 preg_replace 函数中正确转义内容

You can do this with two passes, one for the unbracketed style and one for the bracketed style.

For brackets, replace

'\s*\.\s*\$(?!txt|helptxt)(\w+)\['(\w+)'\]\s*\.\s*'  

with

{\$1[$2]}

Then for the unbracketed variables, replace

'\s*\.\s*\$(?!txt|helptxt)(\w+)\s*\.\s*' 

with

{\$1}

You'll need to make sure you escape things right in the preg_replace function

撩心不撩汉 2024-10-15 05:26:54

根据您的需求,我提出了一个更简单的解决方案:

  1. 替换 $txt & $helptxt 替换为一些非常奇怪的内容,例如“_WHAT_IS_THIS_1”和“_WHAT_IS_THIS_2”
  2. 替换字符串中的所有 $variables
  3. 替换“_WHAT_IS_THIS “$txt”的“_1”和“$helptxt”的“_WHAT_IS_THIS_2”

Based on what you want, I propose a more simple work-around solution:

  1. Replace $txt & $helptxt by something very strange, such as "_WHAT_IS_THIS_1" and "_WHAT_IS_THIS_2"
  2. Replace all the $variables in the string
  3. Replace "_WHAT_IS_THIS_1" by "$txt" and "_WHAT_IS_THIS_2" by "$helptxt"
酒几许 2024-10-15 05:26:54
preg_replace("!\\'\\s*\\.\\s*\\$((?:([^ht]|t[^x]|tx[^t]|h[^e]|he[^l]|hel[^p]|help[^t]|helpt[^x]|helptx[^t])\\w*|helptxt\\w+|txt\\w+)(?:\\[.+?\\])?)\\s*\\.\\s*\\'!", '{$\\1}', $str)

正则表达式并不简单。

preg_replace("!\\'\\s*\\.\\s*\\$((?:([^ht]|t[^x]|tx[^t]|h[^e]|he[^l]|hel[^p]|help[^t]|helpt[^x]|helptx[^t])\\w*|helptxt\\w+|txt\\w+)(?:\\[.+?\\])?)\\s*\\.\\s*\\'!", '{$\\1}', $str)

The regexp is not simple.

星星的轨迹 2024-10-15 05:26:54

它基本上是 PHP 表达式到 Smarty 的转换。对于这个简单的情况并捕获所有潜在变量,请使用:

$data = preg_replace("/'[\s.]+([$]\w+)[\s.]+'/ims", '{$1}', $data);
$data = preg_replace("/'[\s.]+([$]\w+)\['(\w+)'\][\s.]+'/ims", '{$1[$2]}', $data);

反转该过程并将 'string ... {$var[xyz]} ...' 返回为 PHP 表达式 '细绳 ... ' 。 $var["xyz"] 。 ' ...' 使用类似以下内容:

$string = var_dump($string, 1);
$string = preg_replace('/\{([$]\w+)\}/', "' . $1 . '", $data);
$string = preg_replace('/\{([$]\w+)\[(\w+)\]\}/', "' . $1['$2'] . '", $data);

It's basically a PHP expression to Smarty conversion. For this simple case and to catch all potential variables use:

$data = preg_replace("/'[\s.]+([$]\w+)[\s.]+'/ims", '{$1}', $data);
$data = preg_replace("/'[\s.]+([$]\w+)\['(\w+)'\][\s.]+'/ims", '{$1[$2]}', $data);

To reverse the process and turn a 'string ... {$var[xyz]} ...' back into a PHP expression 'string ... ' . $var["xyz"] . ' ...' use something like:

$string = var_dump($string, 1);
$string = preg_replace('/\{([$]\w+)\}/', "' . $1 . '", $data);
$string = preg_replace('/\{([$]\w+)\[(\w+)\]\}/', "' . $1['$2'] . '", $data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文