用PHP从字符串中提取值

2022-03-29 00:00:00 php preg-match

可能的重复项:
Preg_match_all <a href
How to parse and process HTML with PHP?

我使用cURL提取了页面的源代码,需要从cURL输出中提取一些值。

部分输出如下所示:

<div class="detailInfo">
<label>Manufacturer code/Gas council no:
                </label>BKSWX5506</div>
<div class="detailInfo"></div>
<div class="detailInfo">
<div>
<label>Retail price:</label><span>£12.30</span>
</div>
<div>
<label>Net buying price:</label><span>£7.47</span>
</div>
</div>

从该输出中,我需要获得"制造商代码/天然气委员会编号:"之后的代码,并且这两个价格都在单独的字符串中。

有人能帮我吗?

谢谢:)


解决方案

试试:

<?php

        $output = '<div class="detailInfo">
<label>Manufacturer code/Gas council no:
                </label>BKSWX5506</div>
<div class="detailInfo"></div>
<div class="detailInfo">
<div>
<label>Retail price:</label><span>£12.30</span>
</div>
<div>
<label>Net buying price:</label><span>£7.47</span>
</div>
</div>';



        $outputArray = explode("</label>", str_replace("<label>","</label>",strip_tags($output, '<label>')));

        echo "<pre>";
        print_r($outputArray);
        echo "</pre>";
        exit;
?>

输出:

Array
(
    [0] => 

    [1] => Manufacturer code/Gas council no:

    [2] => BKSWX5506




    [3] => Retail price:
    [4] => £12.30



    [5] => Net buying price:
    [6] => £7.47


)

相关文章