Extract value from an HTML tag with PHP

1

I need to know if using explode or substr I can get the values between the span tags of the following loop code. What would be the appropriate syntax? THANK YOU!

Edit: all HTML computation is a field obtained from the bbdd

    <div id="grupo"><span>5</span><p>Invitacion de boda calendario 1</p><p>Cantidad : 1</p></div>
    <div id="grupo"><span>6</span><p>Invitacion de boda calendario 2</p><p>Cantidad : 1</p></div>
    <div id="grupo"><span>7</span><p>Invitacion de boda calendario 3</p><p>Cantidad : 1</p></div>
    
asked by rafa_pe 13.02.2017 в 17:41
source

1 answer

1

Assuming you bring a string independent so you say:

  

... get the values between the span tags of the following loop code ...

     

all the HTML computation is a field obtained from the bbdd

You would have two solutions with both the explode and the substr as follows:

<?php 

$HTML = array(
    '<div id="grupo"><span>5</span><p>Invitacion de boda calendario 1</p><p>Cantidad : 1</p></div>',
    '<div id="grupo"><span>6</span><p>Invitacion de boda calendario 2</p><p>Cantidad : 1</p></div>',
    '<div id="grupo"><span>7</span><p>Invitacion de boda calendario 3</p><p>Cantidad : 1</p></div>',
    );

foreach($HTML as $element){
    $explode = explode("</span>", $element);
    $value = $explode[0];
    echo $value . "<br />";
}

echo "<hr />";

foreach($HTML as $element){
    echo substr($element, 0, 33);
}

?>

Which would show a result more or less like this (the corresponding values within the <span> tag):

5
6
7

5

6

7

    
answered by 13.02.2017 / 18:08
source