php domxpath html,PHP DOM / XPath

博主分享了如何使用PHP脚本从旧的Web文档中解析含有联系详情的表格。目前能抓取包含‘details’类的数据,但还需要完善以获取空字段和网址。文件大约有20-40个条目,寻求帮助完成整个解析过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hopefully should be a simple question for someone that has done it before!

I have a list of old web documents in table format with lots of contact details in it. What I have managed so far is to create a PHP script that parses the XHTML doc and pull out old client contact details.

An example of the document format:

Indigo Blue 123 123 Blue House Hanley ST13 4SN Stoke on Trent 01875 322511 www.indigoblue123.org.uk

What I need to do is parse all of these contact details into an array. The few things that I'm not sure on how to complete is grabbing the empty blocks to be empty array entries (i.e. Address 2 and Address 3 will be blank but I need to know this) as well as grabbing the web address from the .. block.

So far I have figured all populated data has class=details in some form. However, as I mentioned before I'm not sure what the best way to accomplish the overall result is. There around 20-40 entries in the different files I have.

I have managed the basics with this so far:

print '

';

$html = file_get_contents('old-contacts.xhtml');

// Create new DOM object:

$dom = new DomDocument();

// Load HTML code:

$dom->loadHTML($html);

$xpath = new DOMXPath($dom);

$details = $xpath->query("//table/tbody/tr[td/font/@class = 'details']");

for ($i = 0; $i < $details->length; $i++) {

$data[$i]['data'] = $details->item($i)->nodeValue;

echo $data[$i]['data'];

}

print '

';

?>

Any help would be great!

Thanks

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值