0

我有一种情况,客户需要显示在他的页脚上的链接集,但每个页面都需要一个完全不同的链接集。所以我们决定我们将所有的链接集放在数组中,并在一个快速的 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 多个工时,并且必须有一个更简单的解决方案。我不是要求有人为我编写脚本,只是给我一个“可理解”的想法,可以在数小时内实施。

谢谢迪伦
_

4

3 回答 3

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);
    }
?>

这将为您提供链接并输出它们

于 2012-09-18T21:02:36.800 回答
0

刮掉它。PHP 简单的 HTML DOM 解析器。

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

于 2012-09-18T20:56:53.187 回答
0

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

于 2012-09-18T21:02:38.560 回答