php 使用php从h1标签中获取所有值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3299033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting all values from h1 tags using php
提问by Pieter888
I want to receive an array that contains all the h1 tag values from a text
我想接收一个包含文本中所有 h1 标签值的数组
Example, if this where the given input string:
例如,如果这其中给定的输入字符串:
<h1>hello</h1>
<p>random text</p>
<h1>title number two!</h1>
I need to receive an array containing this:
我需要接收一个包含这个的数组:
titles[0] = 'hello',
titles[1] = 'title number two!'
I already figured out how to get the first h1 value of the string but I need all the values of all the h1 tags in the given string.
我已经想出了如何获取字符串的第一个 h1 值,但我需要给定字符串中所有 h1 标签的所有值。
I'm currently using this to receive the first tag:
我目前正在使用它来接收第一个标签:
function getTextBetweenTags($string, $tagname)
{
$pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
I pass it the string I want to be parsed and as $tagname I put in "h1". I didn't write it myself though, I've been trying to edit the code to do what I want it to but nothing really works.
我将要解析的字符串传递给它,并作为 $tagname 放入“h1”。虽然我不是自己编写的,但我一直在尝试编辑代码以执行我想要的操作,但没有任何效果。
I was hoping someone could help me out.
我希望有人可以帮助我。
Thanks in advance.
提前致谢。
回答by Sergey Eremin
you could use simplehtmldom:
你可以使用simplehtmldom:
function getTextBetweenTags($string, $tagname) {
// Create DOM from string
$html = str_get_html($string);
$titles = array();
// Find all tags
foreach($html->find($tagname) as $element) {
$titles[] = $element->plaintext;
}
}
回答by Wrikken
function getTextBetweenTags($string, $tagname){
$d = new DOMDocument();
$d->loadHTML($string);
$return = array();
foreach($d->getElementsByTagName($tagname) as $item){
$return[] = $item->textContent;
}
return $return;
}
回答by Gordon
Alternative to DOM. Use when memory is an issue.
DOM 的替代品。当内存有问题时使用。
$html = <<< HTML
<html>
<h1>hello<span>world</span></h1>
<p>random text</p>
<h1>title number two!</h1>
</html>
HTML;
$reader = new XMLReader;
$reader->xml($html);
while($reader->read() !== FALSE) {
if($reader->name === 'h1' && $reader->nodeType === XMLReader::ELEMENT) {
echo $reader->readString();
}
}
回答by Ahmed Aman
function getTextBetweenH1($string)
{
$pattern = "/<h1>(.*?)<\/h1>/";
preg_match_all($pattern, $string, $matches);
return ($matches[1]);
}

