使用DOM查找HTML中的一行文本/字符串 [英] Finding a line/string of text in HTML using DOM

查看:150
本文介绍了使用DOM查找HTML中的一行文本/字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些纯文本/ HTML内容,如下所示:

I have some Plain Text/HTML Content like so:

Title: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Snippet: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Category: Lorem ipsum dolor sit amet, consectetur adipiscing elit.

,而我只想匹配显示 代码段:的行以及其后的文本,但仅此行,没有其他内容,并且使搜​​索不区分大小写。我尝试使用正则表达式,但最终我想现在尝试使用DOMDocument,如何我这样做吗?

and i want to match only the line where it says "Snippet: and the text that follows it, BUT ONLY on that line, nothing else, and also making the search case-insensitive. I tried with regular expressions, but ultimately i want to attempt using DOMDocument now, how can i do this?

推荐答案

我不知道有关您的问题的一些详细信息,因此我的答案可能不合适。根据需要解析的内容的大小来决定这不是一个选择,另外,从这个问题还不清楚html内容在何处放置,这就是为什么我编写了不使用DOM解析的解决方案的原因。

I don't know some details about your problem, so my answer might not be appropriate. You could decide based on the size of the content you need to parse that this is not an option. Also, from the question it is not clear where the html content comes into place, that is why I wrote this solution that doesn't use DOM parsing.

一种可能的解决方案可能是将要解析的行放入数组中,然后可以过滤该数组,删除不匹配的行您从结果中得出的规则。

A possible solution might be to get the lines that you want to parse in an array. After that you can filter the array, removing the lines that don't match your rule from the result.

示例为:

//this is the content
$text = 'Title: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Snippet: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Category: Lorem ipsum dolor sit amet, consectetur adipiscing elit.';

//get the lines from your input as an array.. you could acheive this in a different way if, for example, you are reading from a file
$lines = explode(PHP_EOL, $text);

// apply a cusom function to filter the lines (remove the ones that don't match your rule)
$results = array_filter($lines, 'test_content');

//show the results
echo '<pre>';
print_r($results);
echo '</pre>';

//custom function here:
function test_content($line)
{
    //case insensitive search, notice stripos; 
    // type strict comparison to be sure that it doesn't fail when the element is found right at the start
    if (false !== stripos($line, 'Snippet'))
    {
        return true;
    }
    return false;//these lines will be removed 
}

那段代码将只返回$ results数组中的一个元素,第二行

that piece of code will return only one element in the $results array, the second line

您可以在这里看到它: http://codepad.org/220BLjEk

you can see it at work here: http://codepad.org/220BLjEk

这篇关于使用DOM查找HTML中的一行文本/字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆