PHP/regex:如何获取 HTML 标签的字符串值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/828870/
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
PHP/regex: How to get the string value of HTML tag?
提问by marknt15
I need help on regex or preg_matchbecause I am not that experienced yet with regards to those so here is my problem.
我需要正则表达式方面的帮助,或者preg_match因为我在这些方面还没有经验,所以这是我的问题。
I need to get the value "get me" but I think my function has an error. The number of html tags are dynamic. It can contain many nested html tag like a bold tag. Also, the "get me" value is dynamic.
我需要获取值“get me”,但我认为我的函数有错误。html 标签的数量是动态的。它可以包含许多嵌套的 html 标签,如粗体标签。此外,“get me”值是动态的。
<?php
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname>(.*?)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
$str = '<textformat leading="2"><p align="left"><font size="10">get me</font></p></textformat>';
$txt = getTextBetweenTags($str, "font");
echo $txt;
?>
回答by takete.dk
<?php
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
$str = '<textformat leading="2"><p align="left"><font size="10">get me</font></p></textformat>';
$txt = getTextBetweenTags($str, "font");
echo $txt;
?>
That should do the trick
这应该够了吧
回答by pkwebmarket
Try this
尝试这个
$str = '<option value="123">abc</option>
<option value="123">aabbcc</option>';
preg_match_all("#<option.*?>([^<]+)</option>#", $str, $foo);
print_r($foo[1]);
回答by Tomas Aschan
In your pattern, you simply want to match all textbetween the two tags. Thus, you could use for example a [\w\W]to match all characters.
在您的模式中,您只想匹配两个标签之间的所有文本。因此,您可以使用例如 a[\w\W]来匹配所有字符。
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname>([\w\W]*?)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
回答by Gumbo
Since attribute values may contain a plain >character, try this regular expression:
由于属性值可能包含普通>字符,请尝试以下正则表达式:
$pattern = '/<'.preg_quote($tagname, '/').'(?:[^"'>]*|"[^"]*"|\'[^\']*\')*>(.*?)<\/'.preg_quote($tagname, '/').'>/s';
But regular expressions are not suitable for parsing non-regular languages like HTML. You should better use a parser like SimpleXMLor DOMDocument.
但是正则表达式不适合解析像 HTML 这样的非正则语言。你最好使用像SimpleXML或DOMDocument这样的解析器。
回答by Gumbo
The following php snippets would return the text between html tags/elements.
以下 php 片段将返回 html 标签/元素之间的文本。
regex : "/tagname(.*)endtag/" will return text between tags.
regex : "/tagname(.*)endtag/" 将返回标签之间的文本。
i.e.
IE
$regex="/[start_tag_name](.*)[/end_tag_name]/";
$content="[start_tag_name]SOME TEXT[/end_tag_name]";
preg_replace($regex,$content);
It will return "SOME TEXT".
它将返回“一些文本”。
Regards,
问候,
Web-Farmer @letsnurture.com
网络农民@letsnurture.com
回答by Xman Classical
$userinput = "http://www.example.vn/";
//$url = urlencode($userinput);
$input = @file_get_contents($userinput) or die("Could not access file: $userinput");
$regexp = "<tagname\s[^>]*>(.*)<\/tagname>";
//==Example:
//$regexp = "<div\s[^>]*>(.*)<\/div>";
if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
// $match[2] = link address
// $match[3] = link text
}
}
回答by Darren Li
try $pattern = "<($tagname)\b.*?>(.*?)</\1>"and return $matches[2]
尝试$pattern = "<($tagname)\b.*?>(.*?)</\1>"和return $matches[2]

