Question

i want to get the order number from below string.

$str = '<table class="adminlist">
<tbody><tr>
<td>Payment Name</td>
 <td align="left"><span class="vmpayment_name">Aurthorize.net</span><span class="vmpayment_description">New payment gateway</span></td>
</tr>
<tr>
<td>Order number</td>
 <td align="left">9b27041</td>
</tr>
<tr>
<td>Amount</td>
 <td align="left">51.30 USD</td>
</tr>
<tr>
<td>Transaction ID</td>
 <td align="left">2200484213</td>
</tr>
</tbody></table>';

How i explode in php. I have tried but not explode . I want order number from above string like

9b27041

And main thing is that all the values is dynamic :(

Was it helpful?

Solution

I think xpath is a good way to extract the value. For this I assume that the position of the order number row is allways the same. The code below does the trick for me.

     <?php
$str = '<table class="adminlist">
    <tbody><tr>
        <td>Payment Name</td>
        <td align="left"><span class="vmpayment_name">Aurthorize.net</span><span class="vmpayment_description">New payment gateway</span></td>
    </tr>
    <tr>
        <td>Order number</td>
        <td align="left">9b27041</td>
    </tr>
    <tr>
        <td>Amount</td>
        <td align="left">51.30 USD</td>
    </tr>
    <tr>
        <td>Transaction ID</td>
        <td align="left">2200484213</td>
    </tr>
    </tbody></table>';


$dom = new DOMDocument();
$dom->loadHTML($str);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//tr[2]/td[2]");

$node = $nodes->item(0);
echo $node->textContent;

OTHER TIPS

Almost impossible to do, PHP cant know where the Order number begins. Try jQuery, give the the class="order_nr" and you can access it easiely.

Only possibility in PHP would be to use strip_tags($str) and then look how to get the order nr from the remaining string.

you can use "PHP Simple HTML DOM parser" its great solution for problems like that, you can read more about it here http://simplehtmldom.sourceforge.net/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top