我将如何开发一个擦除HTML内容然后将其放入数组的PHP脚本

时间:2012-09-18 20:55:59

标签: php regex arrays

我的情况是客户端需要在其页脚上显示的链接集,但每个页面都需要完全不同的链接集。所以我们决定将所有的互连集都放在数组中,并在一个快速的PHP片段中调用它们。成功!它有效,但现在我们有一个更大的问题。随着网站超过500页,输入每个互连集所需的手工劳动将花费很多时间。所以我的目标是开发一个PHP脚本,它将采用一个互连集(见下文)并放入一个数组(见下文),然后我们需要做的就是遍历每一页并查看它是否有一个互连集在页脚如果没有,我们将编写一个快速片段,这将比编程所有相互链接的集合花费更少的时间。


以下是互连集的外观(来自我们的源代码)

<!-- begin_block_7-->
<h2>Cost Reduction Resources</h2>
<a href="http://www.sourceconsulting.com/">shipping charges</a>  ||    
<a href="http://www.sourceconsulting.com/shipping-costs-calculators/fedex-ups-rates-calculator">ups shipping</a>  ||    
<a href="http://www.sourceconsulting.com/carrier-contract-negotiation">freight delivery</a>  ||   
<a href="http://www.sourceconsulting.com/parcel-freight-bill-audit">freight payment companies</a>  ||    
<a href="http://www.sourceconsulting.com/shipping-costs-calculators">compare shipping costs</a>  
<!-- end_block_7 -->

我们需要使用相互链接集并将其放入像这样的数组

$interlinking_set_1 = array ( "<a href='http://www.sourceconsulting.com'>cost freight shipping</a><br/>", 
    "<a href='http://www.sourceconsulting.com/carrier-contract-negotiation'>trucking freight rates</a><br/>", 
    "<a href='http://www.sourceconsulting.com/shipping-costs-calculators/fedex-ups-rates-calculator'>ups shipping calculator</a><br/>", 
    "<a href='http://www.sourceconsulting.com/parcel-freight-bill-audit'>air freight carrier</a><br/>",
    "<a href='http://www.sourceconsulting.com/reduce-shipping-costs'>shipping costs</a><br/>",
    "<a href='http://www.sourceconsulting.com/shipping-costs-calculators'>freight rate calculator</a><br/>"
    );

我知道这肯定会使用正则表达式,这就是我将它作为标记包含在内的原因。我完全失去了如何做到这一点。我们有超过500个需要输入到阵列中的互连集。我们已经花费了超过10个小时的时间做这个,必须有一个更简单的解决方案。我不是要求别人为我写剧本,只是给我一个“可理解的”想法,可以用数小时的时间来实现。

感谢
Dyllen

3 个答案:

答案 0 :(得分:3)

<?php
    $html = @file_get_contents($YOUR_URL);

    if(preg_match('/\<\!\-\- begin\_block\_7\-\-\>[\s\S]*\<\!\-\- end\_block\_7 \-\-\>/iU', $html, $linkBlock)){
        preg_match_all('/\<a.*\>.*\<\/a\>/iU', $linkBlock[0], $links);
        var_dump($links);
    }
?>

那将为您提供链接并输出

答案 1 :(得分:0)

刮掉它。 PHP Simple HTML DOM Parser.

这有jQuery样式选择器。迭代,添加到数组。

答案 2 :(得分:0)

如果您熟悉jQuery样式DOM操作,可以使用queryPath