简体   繁体   English

如何将正则表达式标记转换为 PHP 中字符串中的所有匹配项?

[英]How to perform regex tag conversions to all occurrences in a string in PHP?

I want to convert my html strings which contain <p style=“text-align:center; others-style:value;”>Content</p>我想转换我的 html 字符串,其中包含<p style=“text-align:center; others-style:value;”>Content</p> <p style=“text-align:center; others-style:value;”>Content</p> to <center>Content</center> , and <p style=“text-align:right; others-style:value;”>Content</p> <p style=“text-align:center; others-style:value;”>Content</p><center>Content</center><p style=“text-align:right; others-style:value;”>Content</p> <p style=“text-align:right; others-style:value;”>Content</p> to <right>Content</right> etc. <p style=“text-align:right; others-style:value;”>Content</p><right>Content</right>等等。

My previous question has an answer which can achieve this perfectly.我之前的问题有一个可以完美实现这一点的答案。 Which is regex is:正则表达式是:

$RegEx = '/<(.*)(text-align:)(.*)(center|left|right|justify|inherit|none)(.*)(\"|\”|\'|\’)>(.*)(<\/.*)/s';
$string = preg_replace($RegEx, '<$4>$7</$4>', $string);

However, my strings may contain more than one occurrences of the text-align.但是,我的字符串可能包含多次出现的 text-align。 For example, I might have <div style='text-align:left; others-style:value;' class='any class'>Any Content That You Wish</div><p style=“text-align:center; others-style:value;”>Content</p>例如,我可能有<div style='text-align:left; others-style:value;' class='any class'>Any Content That You Wish</div><p style=“text-align:center; others-style:value;”>Content</p> <div style='text-align:left; others-style:value;' class='any class'>Any Content That You Wish</div><p style=“text-align:center; others-style:value;”>Content</p> <div style='text-align:left; others-style:value;' class='any class'>Any Content That You Wish</div><p style=“text-align:center; others-style:value;”>Content</p> I want it to become <left>Any Content That You Wish</left><center>Content</center> , but it would just output <center>Content</center> . <div style='text-align:left; others-style:value;' class='any class'>Any Content That You Wish</div><p style=“text-align:center; others-style:value;”>Content</p>我希望它变成<left>Any Content That You Wish</left><center>Content</center> ,但它只会输出<center>Content</center>

How can I get what I want in PHP?我怎样才能在 PHP 中得到我想要的东西? Many thanks.非常感谢。

As I wrote in the comments, you should not use regex for manipulating html content.正如我在评论中所写,您不应该使用正则表达式来操作 html 内容。 But only if you have non-nested tags in your html, you can go for it.但只有当您的 html 中有非嵌套标签时,您才能使用它。

For this particular case, you can use this regex,对于这种特殊情况,您可以使用此正则表达式,

<(p|div).*?\bstyle\s*=\s*.*?text-align:([a-zA-Z]+).*?>(.*?)</\1>

And replace it with <\\2>\\3</\\2>并将其替换为<\\2>\\3</\\2>

Regex Demo正则表达式演示

PHP code demo PHP代码演示

$html = '<p style=“text-align:center; others-style:value;”>Content</p>
<div style=‘text-align:left; others-style:value;’ class=‘any class’>Any Content That You Wish</div><p style=“text-align:center; others-style:value;”>Content</p>';

$newhtml = preg_replace("~<(p|div).*?\bstyle\s*=\s*.*?text-align:([a-zA-Z]+).*?>(.*?)</\\1>~", '<\2>\3</\2>', $html);
echo $newhtml;

Prints,印刷,

<center>Content</center>
<left>Any Content That You Wish</left><center>Content</center>

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

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