使用 preg_match 提取 html 正文标记中的内联样式


Extract the inline style in html body tag using preg_match

我想提取包含在html body标签中的字符串。

示例输入:

<html>
    <head>
    </head>
         <body style="color:red" >
           hello
           <p>test</p>
           <div>how r u</div>
         </body>
    </html>

输出:

style="color:red" 

我需要第一次出现的内容>

我在preg_match中使用了这个/body(.*)>/

<?php
$sourcestring="your source string";
preg_match_all('/style="(.*?)"/is',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>

也许是这样的

$html = '<html>
    <head></head>
    <body class="home blog" style="color:red;">
        example
        <p> test </p>
    </body>
';
$tes = preg_match('/'<body(.*?)'>/', $html, $match);
echo $match[1];

将输出

class="home blog" style="color:red;"