我有一个关于 preg_replace 问题的快速问题。我只是正则表达式的新手。
我想要实现的目标如下:
- 我有一个 DIV 标签
sourcefile
- 我想要提取(数据信息)值和(类)值
- 可能有可选标签,但我不需要这些属性的值此
- 替换应该在我拥有的一个字符串中多次工作
:
$input = '<div data-info="sourcefile.ext" class="elm swf">sourcefile</div>';
$input = preg_replace('/(<div\s(class="(.*?)")\s(data-info="(.*?)")\b[^>]*>)(.*?)<\/div>/i', "$1 class:$2 data-info:$3", $input);
我想将这些值用作: <代码><对象src="(data-info)" type="(class)">
这可能吗?有人可以向我展示/解释这是如何工作的吗?
非常感谢。
I have a quick question about a preg_replace problem I have. I am just a newbie in RegEx.
What I would like to achieve is the following:
- I have a DIV tag
<div data-info="sourcefile.ext" class="elm swf">sourcefile</div>
- I would like to extract the (data-info) value and the (class) value
- There might be optional tags but I don't need te value of these attributes
- This replacement should work multiple times in one string
I have:
$input = '<div data-info="sourcefile.ext" class="elm swf">sourcefile</div>';
$input = preg_replace('/(<div\s(class="(.*?)")\s(data-info="(.*?)")\b[^>]*>)(.*?)<\/div>/i', "$1 class:$2 data-info:$3", $input);
I want to use the values as: <object src="(data-info)" type="(class)">
Is this possible? And can somebody show/explain me how this works?
Thank you very much.
发布评论
评论(1)
您的正则表达式失败,因为它以错误的顺序列出了属性。匹配模式不适合此类事情(这将是更喜欢使用 DOM 解析器来实现此类目的的实际原因。)
\b
转义符放错了位置。您可以将这两个属性包装到(?: .. | .. )+
中,以允许一点歧义:$1 $2 $3 编号已关闭,也许您想使用 命名捕获组在这里。
Your regex fails because it lists the attributes in the wrong order. The match pattern does not accomodate for such things (which would be an actual reason to prefer using a DOM parser for such purposes.)
The
\b
escape is misplaced. And you can wrap the two attributes into(?: .. | .. )+
to allow for a little ambiguity:The $1 $2 $3 numbering was off, and maybe you want to use named capture groups here anyway.