将部分html代码解压缩到php变量中

时间:2015-04-22 01:30:55

标签: php html

嗨我需要提取一个html代码,事情就是位置总是改变所以数字改变我试过这样的东西但它不会工作

<?
$html = '<tr><td class='oddrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.1';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='oddrow'  colspan='2'>dsldevice</td><td class='oddrow' colspan='4'>0.0.0.0</td></tr>
<tr><td class='evenrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.3';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='evenrow' colspan='2'>Windows-Phone</td><td class='evenrow' colspan='4'>192.168.1.80</td></tr>
<tr><td class='oddrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.4';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='oddrow'  colspan='2'>iPhone</td><td class='oddrow' colspan='4'>192.168.1.84</td></tr>
<tr><td class='evenrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.5';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='evenrow' colspan='2'>iPhonedNMI15787</td><td class='evenrow' colspan='4'>192.168.1.65</td></tr>
<tr><td class='oddrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.6';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='oddrow'  colspan='2'>iPad</td><td class='oddrow' colspan='4'>192.168.1.83</td></tr>
<tr><td class='evenrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.7';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='evenrow' colspan='2'>iPhonedeLenika</td><td class='evenrow' colspan='4'>192.168.1.86</td></tr>
<tr><td class='oddrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.8';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='oddrow'  colspan='2'>android-1f3dff628fd6e16a</td><td class='oddrow' colspan='4'>192.168.1.68</td></tr>
<tr><td class='evenrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.9';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='evenrow' colspan='2'>android-54839867dc44b7cf</td><td class='evenrow' colspan='4'>192.168.1.72</td></tr>
<tr><td class='oddrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.10';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='oddrow'  colspan='2'>android-f2e92226ea27ecc</td><td class='oddrow' colspan='4'>192.168.1.73</td></tr>
<tr><td class='evenrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.11';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='evenrow' colspan='2'>android-48ef3fc44027c13a</td><td class='evenrow' colspan='4'>192.168.1.75</td></tr>
<tr><td class='oddrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.12';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='oddrow'  colspan='2'>www</td><td class='oddrow' colspan='4'>192.168.1.78</td></tr>'

preg_match('/DNS.Server.Host.\d+\';submitFormRelf(\'DNS\');\" src=\"/images/bull__dr.gif\" alt=\"\"></td><td class=\'oddrow\'  colspan=\'2\'>www/\', $html, $match);
print_r ($match);
?>

因为你可以看到我需要从 www 行中提取数字,所以在这种情况下是数字 12 。有时它会改变,我希望你能帮助我

1 个答案:

答案 0 :(得分:0)

您的$html变量混合使用单引号和双引号。

$html = <<<LOB
<tr><td class='oddrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.1';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='oddrow'  colspan='2'>dsldevice</td><td class='oddrow' colspan='4'>0.0.0.0</td></tr>
<tr><td class='evenrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.3';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='evenrow' colspan='2'>Windows-Phone</td><td class='evenrow' colspan='4'>192.168.1.80</td></tr>
<tr><td class='oddrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.4';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='oddrow'  colspan='2'>iPhone</td><td class='oddrow' colspan='4'>192.168.1.84</td></tr>
<tr><td class='evenrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.5';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='evenrow' colspan='2'>iPhonedNMI15787</td><td class='evenrow' colspan='4'>192.168.1.65</td></tr>
<tr><td class='oddrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.6';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='oddrow'  colspan='2'>iPad</td><td class='oddrow' colspan='4'>192.168.1.83</td></tr>
<tr><td class='evenrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.7';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='evenrow' colspan='2'>iPhonedeLenika</td><td class='evenrow' colspan='4'>192.168.1.86</td></tr>
<tr><td class='oddrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.8';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='oddrow'  colspan='2'>android-1f3dff628fd6e16a</td><td class='oddrow' colspan='4'>192.168.1.68</td></tr>
<tr><td class='evenrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.9';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='evenrow' colspan='2'>android-54839867dc44b7cf</td><td class='evenrow' colspan='4'>192.168.1.72</td></tr>
<tr><td class='oddrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.10';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='oddrow'  colspan='2'>android-f2e92226ea27ecc</td><td class='oddrow' colspan='4'>192.168.1.73</td></tr>
<tr><td class='evenrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.11';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='evenrow' colspan='2'>android-48ef3fc44027c13a</td><td class='evenrow' colspan='4'>192.168.1.75</td></tr>
<tr><td class='oddrow' ><img class="hand" onclick="document.dns_config.host.value = 'DNS.Server.Host.12';submitFormRelf('DNS');" src="/images/bull__dr.gif" alt=""></td><td class='oddrow'  colspan='2'>www</td><td class='oddrow' colspan='4'>192.168.1.78</td></tr>'
LOB;

preg_match_all('/DNS\.Server\.Host\.(\d+).*?>www</i', $html , $match, PREG_PATTERN_ORDER);
$host = $match[1][0];
echo $host;

输出:

12

DEMO