0

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>
Muazam
  • 379
  • 1
  • 6
  • 15
  • possible duplicate of [Extract DOM-elements from string, in PHP.](http://stackoverflow.com/questions/5126967/extract-dom-elements-from-string-in-php) – Marc B Apr 22 '11 at 19:19
  • My answer has a complete example of using DOMDocument – adam Apr 22 '11 at 19:29

3 Answers3

2

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.

Brett Zamir
  • 14,034
  • 6
  • 54
  • 77
  • I'm getting this error "Undefined offset: 0" any idea? on line "$text2 = $matches[0];" – Muazam Apr 22 '11 at 19:39
  • Yeah, your $text string didn't have `....` apparently. You can try it out with my updated sample above (which also deals with the offset issue by properly checking whether any matches were found at all). – Brett Zamir Apr 22 '11 at 19:46
  • Thanks, it worked. but do you think I can get the tags as well? And get something like a embed tag? Thanks a lot! – Muazam Apr 22 '11 at 20:07
  • For embed, just do the same thing as above, but with "embed" replacing each use of "b". I'm not sure how you want the tags, but it seems like you need to read up a little more on regular expressions and preg_match so you can craft these as you need them. The above shows the general approach. Good luck! – Brett Zamir Apr 22 '11 at 20:11
  • EXCELLENT! Thanks for the help the code works! Thanks a lot, I really appreciate it! – Muazam Apr 22 '11 at 21:57
1

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;
adam
  • 3,888
  • 2
  • 20
  • 15
  • It is worth noting that the text needs to be valid dom content, otherwise xpath will be angry at you, your string, your program, your users and your web host – adam Apr 22 '11 at 19:31
  • Adam@ I get this error "Call to undefined function loadHTML()" Thanks for helping. Will this work on the embed code aswell? – Muazam Apr 22 '11 at 19:35
  • Haha, my bad Muazam. I'm a Java guy by day, which is why I wrote $dom.loadHTML, when it should be $dom->loadHTML. You should definately use this approach over the selected one because its all OO and short and easy on the eyes – adam Apr 22 '11 at 23:36
-1
strip_tags($text, '<b>');

will extract only the parts of the string between <b> </b>

If it is the behavior you look for.

Marc Bouvier
  • 642
  • 1
  • 11
  • 27
  • Unfortunately, that will return all text in the string that isn't part of a php or html tag (or NUL char). What he's looking for is just getting the inner content of the s – adam Apr 22 '11 at 19:28