我意识到PHP没有多维数组,但问题就在这里。
我在for($ fix)循环中有这个:
$html->find('tr[class] td', $fix);
现在连续抓取了多个td,所以说第一个拿着用户名
我想获取用户名,这就是我正在尝试的但它不起作用:
$username = $html[0];
我也试过这样的事情
$username = $html->find('tr[class] td', $fix, 0)
这些都不起作用,我应该使用什么?
完整代码:
<?php
include('simple_html_dom.php');
$q=$_REQUEST["q"]; $hint="";
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 15
)
)
);
$html = file_get_html('http://www.example.com/index.php?q=' . urlencode($q), 0, $ctx);
$tr = $html->find('tr');
// lookup all hints from array if $q is different from ""
$fix = 0;
if ($q !== "") {
$q=strtolower($q); $len=strlen($q);
foreach($tr as $name) {
$fix++;
if($fix > 2) {
$selector = $html->find('tr[class] td');
$username = $selector[$fix][0]->plaintext;
$name = $selector;
$username_fix = str_replace ('.',"",$username);
if ($hint==="") {
$hint='<tr>
<td>' . $name->plaintext . '<br />' . $username . '</td>
<td> </td>
<td><button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#M' . $username_fix . '">This is me!</button></td>
<div class="modal fade" id="M' . $username_fix . '" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Yo</h4>
</div>
<div class="modal-body">
<p>Hey…' . $name->plaintext . '</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</tr>';
} else {
$hint .='<tr>
<td>' . $name->plaintext . '<br />' . $username . '</td>
<td> </td>
<td><button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#M' . $username_fix . '">This is me!</button></td>
<div class="modal fade" id="M' . $username_fix . '" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Yo</h4>
</div>
<div class="modal-body">
<p>Hey…' . $name->plaintext . '</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</tr>';
}
}
}
}
// Output "no suggestion" if no hint were found
// or output the correct values
if($hint === "") {
echo 'My records indicate you a liar!';
} else {
echo '
<table class="table table-striped table-bordered">
<tr>
<th style="min-width:20%;">Name</th>
<th>Department</th>
<th width="9%"> </th>
</tr>
';
echo $hint;
echo '</table>';
}
?>
仍然很脏,希望它首先发挥作用