0

I want to get all the substring contents in one special html tag, in the example its

<b></b>:

 function getTextBetweenTags($string, $tagname) {
   $pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
   preg_match($pattern, $string, $matches);
   return $matches;
}

$message = "<p> Te informamos que la parada <b> Avenida de la Vega </b> 
  se ha llenado, el día <b>2013-04-22 </b> a las <b>08:23:27</b>.
  <br><br> No olvides cerrar este ticket cuando hayas resuelto incidencia.
  <br><br> Gracias </p>";


 $result = getTextBetweenTags($message, "b");
 var_dump($txt);

I get:

array(2) {
  [0]=>
   string(90) "<b> Avenida de la Vega </b> se ha llenado, el día <b>2013-04-22 </b> a las <b>08:23:27</b>"
  [1]=>
  string(8) "08:23:27"
 }

And I would like:

array(3) {
  [0]=>
   string(20) "Avenida de la Vega" 
   [1]=>
    string(10) "2013-04-22"
   [2]=>
    string(8) "08:23:27"
  }

How can I get it?

2
  • Where is the $txt variable coming from? Commented Apr 22, 2013 at 14:52
  • Am I missing something or are you... Commented Apr 22, 2013 at 14:53

1 Answer 1

1

Parsing HTML should not be done via RegEx. Better use DOM like this:

$html='
<p> Te informamos que la parada <b> Avenida de la Vega </b> 
  se ha llenado, el día <b>2013-04-22 </b> a las <b>08:23:27</b>.
  <br><br> No olvides cerrar este ticket cuando hayas resuelto incidencia.
  <br><br> Gracias </p>';
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your html
$nodeList = $doc->getElementsByTagName('b');
$items = array();
for($i=0; $i < $nodeList->length; $i++) {
    $node = $nodeList->item($i);
    $items[] = $node->nodeValue;
}
print_r($items);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.