从PHP中的html标签爆炸

问题描述:

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 :(

我想从下面的字符串中获取订单号。 p>

  $ str ='&lt; table class =“adminlist”&gt; 
&lt; tbody&gt;&lt; tr&gt; 
&lt; td&gt;付款名称&lt; / td&gt; 
&lt; td align =“left”&gt;&lt; span  class =“vmpayment_name”&gt; Aurthorize.net&lt; / span&gt;&lt; span class =“vmpayment_description”&gt;新的付款网关&lt; / span&gt;&lt; / td&gt; 
&lt; / tr&gt; 
&lt; tr&gt; 
&lt;  td&gt;订单号&lt; / td&gt; 
&lt; td align =“left”&gt; 9b27041&lt; / td&gt; 
&lt; / tr&gt; 
&lt; tr&gt; 
&lt; td&gt;金额&lt; / td&gt; 
&lt;  td align =“left”&gt; 51.30 USD&lt; / td&gt; 
&lt; / tr&gt; 
&lt; tr&gt; 
&lt; td&gt;交易ID&lt; / td&gt; 
&lt; td align =“left”&gt; 2200484213&lt;  / td&gt; 
&lt; / tr&gt; 
&lt; / tbody&gt;&lt; / table&gt;'; 
  code>  pre> 
 
 

我如何在php中爆炸。 我试过但没有爆炸。 我想要上面字符串的订单号,如 p>

9b27041 p> blockquote>

主要的是所有值 是动态的:( p> div>

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;

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/