Let's say we have a string ($text)
I will help you out, if <b>you see this message and never forget</b> blah blah blah
I want to take text from "<b>
" to "</b>
" into a new string($text2)
How can this be done?
I appreciate any help I can get. Thanks!
Edit: I want to take a code like this.
<embed type="application/x-shockwave-flas开发者_如何学JAVAh"></embed>
If you only wish the first match and do not want to match something like <b class=">
, the following will work:
UPDATED for comment:
$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.
}
But for something more complex, you really should parse it as DOM per Marc B.'s comment.
Use this bad mofo: http://fr2.php.net/domdocument
$dom = new DOMDocument();
$dom->loadHTML($text);
$xpath = new DOMXpath($dom);
$nodes = $xpath->query('//b');
Here you can either loop through each one, or if you know there is only one, just grab the value.
$text1 = $nodes->item(0)->nodeValue;
strip_tags($text, '<b>');
will extract only the parts of the string between <b> </b>
If it is the behavior you look for.
精彩评论