使用preg_match在html body标签中提取内联样式

时间:2015-01-16 07:13:38

标签: php

我想提取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(.*)> /

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

也许是这样的

$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;"