用于HTML标记转换的RegEx [英] RegEx for HTML tag conversion

查看:131
本文介绍了用于HTML标记转换的RegEx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我想转换包含以下内容的字符串

For some reasons, I want to convert strings which contain

<p style="text-align:center; others-style:value;">Content</p>

<center>Content</center>在PHP中.

文本对齐值可以是左,右或居中.还有其他样式时,我想省略它们.

The text-align values can be either left, right, or center. And when there are other stylings, I want to omit them.

如何在PHP中做到这一点?

How can I do that in PHP?

也许我对最初的问题还不够清楚.我的意思是我想将text-align:center的内容转换为<center>的内容,并将text-align:right的内容转换为<right>的内容.而且,当没有文本对齐样式时,该div不需要任何换行.谢谢.

Maybe I was not clear enough in my original question. What I mean is that I want to convert contents with text-align:center to be wrapped by <center>, and contents with text-align:right to be wrapped by <right>. And when there is no text-align styling, I do not need any wrapping for that div. Thank you.

推荐答案

您可以使用preg_replace这样做:

$test = preg_replace('/(<.*">)(.*)(<\/.*)/s', '<center>$2</center>', '<p style="text-align:center; others-style:value;">Content</p>');

var_dump($test);

输出1:

它将返回:

Output 1:

It would return:

string(24) "<center>Content</center>"

RegEx 1:

RegEx 将您的输入分为三个捕获组,第一和第三组可以分配给打开/关闭p标签.

RegEx 1:

The RegEx divides your inputs into three capturing groups, where the first and third groups can be assigned to open/close p tags.

如果需要,您可以使用 RegEx 进一步扩展它,以用于其他标签/您可能想要的报价/内容.它会将带有引号(或"或'或')的任何标签分为五个组,其中第四组( $ 4 )是您的目标内容.非循环字符串,因为它使用(.*).

You can further expand it, if you wish, with this RegEx for any other tags/quotations/contents that you may want. It would divide any tags with any quotations (" or " or ' or ’) into five groups where the fourth group ($4) is your target content. This type of RegEx may be usually useful for single occurrence non-looping strings, since it uses (.*).

$test = preg_replace('/<(.*)(\"|\"|\'|\’)>(.*)(<\/.*)/s', '<center>$4</center>', '<p style="text-align:center; others-style:value;">Content</p>');

var_dump($test);

RegEx 3

如果您希望获取样式中的任何特定属性,则此RegEx 可能会有所帮助:

RegEx 3

If you may wish to get any specific attributes in style, this RegEx might help:

<(.*)(text-align:)(.*)(center|left|right|justify|inherit|none)(.*)(\"|\"|\'|\’)>(.*)(<\/.*)

$tags = [
    '0' => '<p style="text-align:center; others-style:value;">Content</p>',
    '1' => '<div style=‘text-align:left; others-style:value;’ class=‘any class’>Any Content That You Wish</div>',
    '2' => '<span style=\'text-align:right; others-style:value;\' class=\'any class\'>Any Content That You Wish</span>',
    '3' => '<h1 style="text-align:justify; others-style:value;" class="any class">Any Content That You Wish</h1>',
    '4' => '<h2 style="text-align:inherit; others-style:value;" class="any class">Any Content That You Wish</h2>',
    '5' => '<h3 style="text-align:none; others-style:value;" class="any class">Any Content That You Wish</h3>',
    '6' => '<h4 style="others-style:value;" class="any class">Any Content That You Wish</h4>',
];

var_dump($tag);

$RegEx = '/<(.*)(text-align:)(.*)(center|left|right|justify|inherit|none)(.*)(\"|\"|\'|\’)>(.*)(<\/.*)/s';
foreach ($tags as $key => $tag) {
    preg_match_all($RegEx, $tag, $matches);
    foreach ($matches as $key1 => $match) {
        if (sizeof($match[0]) > 0) {
            $tags[$key] = preg_replace($RegEx, '<$4>$7</$4>', $tag);
            break;
        }

    }

}

var_dump($tags);

输出3

它将返回:

Output 3

It would return:

array(7) {
  [0]=>
  string(24) "<center>Content</center>"
  [1]=>
  string(38) "<left>Any Content That You Wish</left>"
  [2]=>
  string(40) "<right>Any Content That You Wish</right>"
  [3]=>
  string(44) "<justify>Any Content That You Wish</justify>"
  [4]=>
  string(44) "<inherit>Any Content That You Wish</inherit>"
  [5]=>
  string(38) "<none>Any Content That You Wish</none>"
  [6]=>
  string(86) "<h4 style="others-style:value;" class="any class">Any Content That You Wish</h4>"
}

这篇关于用于HTML标记转换的RegEx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆