简体   繁体   English

preg_match_ALL 的简单模式可以正常工作吗? 如何与 preg_replace 一起使用?

[英]simple pattern with preg_match_ALL work fine!, how to use with preg_replace?

thanks by your help.感谢您的帮助。

my target is use preg_replace + pattern for remove very sample strings.我的目标是使用preg_replace + pattern来删除非常示例的字符串。

then only using preg_replace in this string or others, I need remove ANY content into <tag and next symbol > , the pattern is so simple, then:然后只在这个字符串或其他字符串中使用preg_replace ,我需要将任何内容删除到<tag和下一个符号> ,模式很简单,然后:

$x = '@<\w+(\s+[^>]*)>@is';
$s = 'DATA<td class="td1">111</td><td class="td2">222</td>DATA';
preg_match_all($x, $s, $Q);
print_r($Q[1]);

[1] => Array
    (
        [0] =>  class="td1"
        [1] =>  class="td2"
    )

work greath!伟大的工作!

now I try remove strings using the same pattern:现在我尝试使用相同的模式删除字符串:

$new_string = '';
$Q = preg_replace($x, "\\1$new_string", $s);
print_r($Q);

result is completely different.结果完全不同。

what is bad in my use of preg_replace ?我使用preg_replace有什么不好?

using only preg_replace() how I can remove this strings?使用 preg_replace() 我如何删除这些字符串?

(we can use foreach(...) for remove each string, but where is the error in my code?) (我们可以使用 foreach(...) 删除每个字符串,但我的代码中的错误在哪里?)

my result expected when I intro this value:当我引入这个值时,我的结果是预期的:

$s = 'DATA<td class="td1">111</td><td class="td2">222</td>DATA';

is this output:这是 output:

$Q = 'DATA<td>111</td><td>222</td>DATA';

Let's break down your RegEx, @<\w+(\s+[^>]*)>@is , and see if that helps.让我们分解您的正则表达式@<\w+(\s+[^>]*)>@is ,看看是否有帮助。

@          // Start delimiter
<          // Literal `<` character
\w+        // One or more word-characters, a-z, A-Z, 0-9 or _
(          // Start capturing group
  \s+      // One or more spaces
  [^>]*    // Zero or more characters that are not the literal `>`
)          // End capturing group
>          // Literal `>` character
@          // End delimiter
is         // Ignore case and `.` matches all characters including newline

Given the input DATA<td class="td1">DATA this matches <td class="td1"> and captures class="td1" .给定输入DATA<td class="td1">DATA匹配<td class="td1">捕获class="td1" The difference between match and capture is very important.匹配捕获之间的区别非常重要。

When you use preg_match you'll see the entire match at index 0 , and any subsequent captures at incrementing indexes.当您使用preg_match时,您将在索引0处看到整个匹配,以及在递增索引处的任何后续捕获

When you use preg_replace the entire match will be replaced.当您使用preg_replace时,整个匹配将被替换。 You can use the captures , if you so choose, but you are replacing the match .如果您愿意,您可以使用captures ,但您要替换match

I'm going to say that again: whatever you pass as the replacement string will replace the entirety of the found match .我要再说一遍:作为替换字符串传递的任何内容都将替换整个找到的match If you say $1 or \\=1 , you are saying replace the entire match with just the capture.如果你说$1\\=1 ,你说的是用捕获替换整个匹配

Going back to the sample after the breakdown, using $1 is the equivalent of calling:分解后回到示例,使用$1相当于调用:

str_replace('<td class="td1">', ' class="td1"', $string);

which you can see here: https://3v4l.org/ZkPFb你可以在这里看到: https://3v4l.org/ZkPFb

To your question "how to change [0] by $new_string ", you are doing it correctly, it is your RegEx itself that is wrong.对于您的问题“如何通过$new_string更改[0] ”,您做得正确,是您的 RegEx 本身是错误的。 To do what you are trying to do, your pattern must capture the tag itself so that you can say "replace the HTML tag with all of the attributes with just the tag".要执行您想做的事情,您的模式必须捕获标签本身,以便您可以说“将 HTML 标记替换为仅包含该标记的所有属性”。

As one of my comments noted, this is where you'd invert the capturing.正如我的评论之一所指出的,这是您反转捕获的地方。 You aren't interesting in capturing the attributes, you are throwing those away.你对捕捉属性不感兴趣,你把它们扔掉了。 Instead, you are interested in capturing the tag itself:相反,您有兴趣捕获标签本身:

$string = 'DATA<td class="td1">DATA';
$pattern = '@<(\w+)\s+[^>]*>@is';

echo preg_replace($pattern, '<$1>', $string);

Demo: https://3v4l.org/oIW7d演示: https://3v4l.org/oIW7d

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

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