提取链接并保存HTML中的位置


Extracting links and saving positions in HTML

我有html,我想完成的是:

  • 扫描文本以查找anchor链接
  • 提取anchor link的详细信息,并在其在html中的位置放置标记
  • 在数据库中保存文本后,将标记替换回与其位置关联的anchor link

因此,以下是起始html:

    <html>
        <body>
            <a href="somelink">Some text</a>
            <p>Some paragraph text</p>
            <a href="someotherlink">Some other text</a>
        </body>
    </html>

这是替换后得到的html:

    <html>
        <body>
            {articleLink0}
            <p>Some paragraph text</p>
            {articleLink1}
        </body>
    </html>

然后,在数据结构中,它将被构建为。正如你所看到的,索引位置被反映出来(例如,{articleLink0}将被腐蚀为$data[0]

    $data = array(
        0 => array('href'=>'somelink','text'=>'Some text'),
        1 => array('href'=>'someotherlink','text'=>'Some other text')
    );

最后,我需要一种方法,将内容放回数据结构中指定的位置

$data数组上循环,生成一个内部带有渲染的a标记的字符串,使用索引生成替换字符串,最后用生成的a标记替换替换字符串。

  • a标记呈现=>常规字符串串联
  • 替换=>常规字符串串联的生成
  • 替换本身=>str_replace

像这样的东西应该起作用:

foreach ($data as $key => $value) {
    // value and href have to be escaped, if they contain unsafe characters
    $aTag = sprintf('<a href="%s">%s</a>', $value['href'], $value['text']);
    $toReplace = sprintf('{articleLink%d}', $key);
    $htmlContent = str_replace($toReplace, $aTag, $htmlContent);
}