使用PHP和xPath按照标签文本存储值?

使用PHP和xPath按照标签文本存储值?,php,xpath,Php,Xpath,我正在尝试存储文本/字符串值。这些值没有唯一标识符。但是,存在唯一的标签 使用xPath和PHP,如何存储值? 第二个问题是,根据记录,扇区标签不包含任何、一个或多个结果 例如: [Location] = London, UK; [Price] = £5000; [Sector] = IT, ICT; HTML: 位置 英国伦敦 价格 £5000 部门 当前代码: foreach ($entries as $entry) { $node = $xpath->query("

我正在尝试存储文本/字符串值。这些值没有唯一标识符。但是,存在唯一的标签

使用xPath和PHP,如何存储值?

第二个问题是,根据记录,扇区标签不包含任何、一个或多个结果

例如:

[Location] = London, UK;
[Price] = £5000;
[Sector] = IT, ICT;
HTML:


位置
英国伦敦
价格
£5000
部门
当前代码:

 foreach ($entries as $entry) {

    $node = $xpath->query("div/a | div/p | div/label | div/span", $entry);

    echo '<job>' . "\n";

    foreach ($node as $i) {
        $tag = $i->nodeName;
        $att = $i->getAttribute('id');
        $string = $i->nodeValue;
        $string = preg_replace('/\s+\s+/','',$string);

....

 echo '<' . $tag . ">" . $string . '</' . $tag . ">" . "\n";
foreach($entries作为$entry){
$node=$xpath->query(“div/a | div/p | div/label | div/span”,$entry);
回显“”。“\n”;
foreach($i节点){
$tag=$i->nodeName;
$att=$i->getAttribute('id');
$string=$i->nodeValue;
$string=preg_replace('/\s+\s+/','','$string);
....

echo'PHP不是我的主要语言,但这里有一种快速方法,首先获取标签,然后使用xpath获取相邻的span元素:

<?php
$string = <<<XML
<div class="DetailsPanel">
    <label class="ListLabel left">Location</label>
    <span id="location" class="ListDetail left" title="London, UK">London, UK</span>
     <label class="ListLabel left">Price</label>
    <span id="price" class="ListDetail left" title="£5000">£5000</span>
    <label class="ListLabel left">Sector</label>
    <span class="ListDetail left">
      <a href="/">IT</a>
      <a href="/">ICT</a>
    </span>
  </div>
XML;

$xml = new SimpleXMLElement($string);

/* get label nodes*/
$label = $xml->xpath('label');

/* iterate over labels */
foreach ($label as $l) {

    /* get adjacent span element */
    foreach ($l->xpath("following-sibling::span[1]") as $span) {        
        $a = "";
        /* if span has a */
        if ($span->xpath("a")) {
            $a = join(", ",$span->xpath("a"));
        }          
    }
    echo $l, " : ", $span,$a, "<br/>";
}
?>

为什么你特别想寻找XPath解决方案?你有一些现有的PHP代码可以给我们看吗?可能有一个更合适的答案涉及HTML解析工具,而不是重新设计的XML工具。不需要是XPath,但这是我一直在使用的。
<?php
$string = <<<XML
<div class="DetailsPanel">
    <label class="ListLabel left">Location</label>
    <span id="location" class="ListDetail left" title="London, UK">London, UK</span>
     <label class="ListLabel left">Price</label>
    <span id="price" class="ListDetail left" title="£5000">£5000</span>
    <label class="ListLabel left">Sector</label>
    <span class="ListDetail left">
      <a href="/">IT</a>
      <a href="/">ICT</a>
    </span>
  </div>
XML;

$xml = new SimpleXMLElement($string);

/* get label nodes*/
$label = $xml->xpath('label');

/* iterate over labels */
foreach ($label as $l) {

    /* get adjacent span element */
    foreach ($l->xpath("following-sibling::span[1]") as $span) {        
        $a = "";
        /* if span has a */
        if ($span->xpath("a")) {
            $a = join(", ",$span->xpath("a"));
        }          
    }
    echo $l, " : ", $span,$a, "<br/>";
}
?>
Location : London, UK
Price : £5000
Sector : IT, ICT