Reputation: 379
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-flash"></embed>
Upvotes: 0
Views: 504
Reputation: 4005
Use this bad mofo: https://www.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;
Upvotes: 1
Reputation: 14345
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.
Upvotes: 2
Reputation: 702
strip_tags($text, '<b>');
will extract only the parts of the string between <b> </b>
If it is the behavior you look for.
Upvotes: -1