我需要有关正则表达式的帮助,或者preg_match因为我不是那么有经验但关于那些,所以这里是我的问题.
我需要获得值"得到我",但我认为我的函数有错误.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;
?>
Run Code Online (Sandbox Code Playgroud)
小智 67
<?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;
?>
Run Code Online (Sandbox Code Playgroud)
这应该够了吧
pkw*_*ket 11
试试这个
$str = '<option value="123">abc</option>
<option value="123">aabbcc</option>';
preg_match_all("#<option.*?>([^<]+)</option>#", $str, $foo);
print_r($foo[1]);
Run Code Online (Sandbox Code Playgroud)
在您的模式中,您只想匹配两个标记之间的所有文本.因此,您可以使用例如a [\w\W]来匹配所有字符.
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname>([\w\W]*?)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101239 次 |
| 最近记录: |