假设我们有一个字符串 ($text)
I will help you out, if <b>you see this message and never forget</b> blah blah blah
我想将文本从 " <b>
" 到 " </b>
" 放入一个新字符串($text2) 怎么做?
我很感激我能得到的任何帮助。谢谢!
编辑:我想采用这样的代码。
<embed type="application/x-shockwave-flash"></embed>
如果您只希望第一次匹配并且不想匹配类似<b class=">
的内容,则以下内容将起作用:
更新评论:
$text = "I will help you out, if <b>you see this message and never forget</b> blah blah blah";
$matches = array();
preg_match('@<b>.*?</b>@s', $text, $matches);
if ($matches) {
$text2 = $matches[0];
// Do something with $text2
}
else {
// The string wasn't found, so do something else.
}
但是对于更复杂的事情,您真的应该按照 Marc B. 的评论将其解析为 DOM。
使用这个坏 mofo: http: //fr2.php.net/domdocument
$dom = new DOMDocument();
$dom->loadHTML($text);
$xpath = new DOMXpath($dom);
$nodes = $xpath->query('//b');
在这里,您可以循环遍历每一个,或者如果您知道只有一个,则只需获取值。
$text1 = $nodes->item(0)->nodeValue;
strip_tags($text, '<b>');
将仅提取字符串之间的部分<b> </b>
如果这是您要寻找的行为。