Finding a line/string of text in HTML using DOM

寵の児 提交于 2020-01-05 09:15:03

问题


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.

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?


回答1:


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.

A sample would be:

//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 
}

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

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




回答2:


In case DOM is concerned, see the duplicate I linked in a comment.

Otherwise you might just look for a regular expression:

$line = preg_match('~(^Snippet:.*$)~m', $text, $matches) ? $matches[1] : NULL;

Demo and Regex Explained:

~  -- delimiter
 (  -- start match group 1
  ^  -- start of line
    Snippet:  -- exactly this text
    .*  -- match all but newline
  $  -- end of line
 )  -- end match group 1
~  -- delimiter
m  -- multiline modifier (^ matches begin of line, $ end of line)


来源:https://stackoverflow.com/questions/10484825/finding-a-line-string-of-text-in-html-using-dom

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!