[英]REGEX - PHP Get only bold section in a string
I am new to Regex. 我是Regex的新手。 I have a string like:
我有一个字符串:
Hello <b>ABCD</b> World
or
<b>ABCD</b>Hello World
I basically want to retain the text inside bold tags but remove all other characters in the string. 我基本上希望将文本保留在粗体标记内,但删除字符串中的所有其他字符。
I have found the code to remove bold part in the string: 我找到了删除字符串中粗体部分的代码:
$string = 'This is <b>an</b> example <b>text</b>';
echo preg_replace('/(<b>.+?)+(<\/b>)/i', '', $string);
So how do I make it to work in opposite way? 那么如何让它以相反的方式工作呢?
Regards Ahmar 问候艾哈迈尔
Use a DOM
parser instead of a regex if you want to extract data from a HTML or XML document. 如果要从HTML或XML文档中提取数据,请使用
DOM
解析器而不是正则表达式。 While a regex will work in simple cases too, it can get weird if the use case gets more complicated or the input data changes in an unexpected way. 虽然正则表达式也可以在简单的情况下工作,但如果用例变得更复杂或输入数据以意想不到的方式发生变化,则会变得奇怪。 A
DOM
parser is more stable and convenient for that purpose. DOM
解析器更稳定,更方便。
Example code: 示例代码:
$doc = new DOMDocument();
$doc->loadHTML('Hello <b>ABCD</b> World');
foreach($doc->getElementsByTagName('b') as $element) {
echo $element->nodeValue;
}
use preg_match_all: 使用preg_match_all:
preg_match_all("'<b>(.*?)</b>'si", $text, $match);
foreach($match[1] as $val)
{
echo $val."<br>";
}
Try this 尝试这个
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
preg_match_all($pattern, $string, $matches);
return $matches[1];
}
$str = 'This is <b>an example text</b>';
$txt = getTextBetweenTags($str, "b");
print_r($txt);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.