使用 CSS 样式选择器从 PHP 的大字符串中提取元素?

发布于 2024-11-08 12:45:37 字数 11982 浏览 0 评论 0原文

我有以下长字符串从 SOAP 调用返回到变量中,我需要查找其中某些元素的内容,即 .MoreInfoZone .OfferZone #Heading .content。 MoreInfoZone .OfferZone #Description .content

我无法使用 JavaScript 来完成此操作,因为它用于填充 Facebook Open Graph 的元标记,并且他们的蜘蛛不会运行 JavaScript。

$soap = '    

<?xml version="1.0" encoding="utf-16"?>


    <div class="MoreInfoZone">
      <div class="OfferZone">
        <div id="Heading" class="Group">
          <div class="Label">
            <h3>Activity</h3>
          </div>
          <div class="Content">5-16 Trampoline / Swim Group 5-16 Trampoline / Swim
          Group5-16 Trampoline / Swim Group5-16 Trampolin1</div>
        </div>
        <div id="WhenZone">
          <div id="Date" class="Group">
            <div class="Label">
              <h3>
                <span class="StartDateLabel">Date</span>
              </h3>
            </div>
            <div class="Content">
            <span class="Start Date">21 December 2010</span> to 
            <span class="End Date">24 November 2011</span></div>
          </div>
          <div id="Time" class="Group">
            <div class="Label">
              <h3>Time</h3>
            </div>
            <div class="Content">
            <span class="Start Time">10:00am</span> to 
            <span class="End Time">12:00am</span></div>
          </div>
          <div id="DaysOfTheWeek" class="Group">
            <div class="Label">
              <h3>
                <span class="DaysOfTheWeekLabel">Days</span>
              </h3>
            </div>
            <div class="Content">
            <span class="weekdays">Monday, Tuesday, Wednesday, Thursday, Friday,</span> 
            <span class="weekend">Saturday, Sunday</span></div>
          </div>
        </div>
        <div id="Description" class="Group">
          <div class="Label">
            <h3>Description</h3>
          </div>
          <div class="Content">
            <p>Trampolining5-16 Trampoline / Swim Group 5-16 Trampoline / Swim Group5-16
            Trampoline / Swim Group5-16 Trampolin15-16 Trampoline / Swim Group 5-16
            Trampoline / Swim Group5-16 Trampoline / Swim Group5-16 Trampolin15-16
            Trampoline / Swim Group 5-16 Trampoline / Swim Group5-16 Trampoline / Swim
            Group5-16 Trampolin15-16 Trampoline / Swim Group 5-16 Trampoline / Swim
            Group5-16 Trampoline / Swim Group5-16 Trampolin15-16 Trampoline / Swim Group
            5-16 Trampoline / Swim Group5-16 Trampoline / Swim Group5-16 Trampolin1</p>
          </div>
        </div>
        <div id="Provider" class="Group">
          <div class="Label">
            <h3>Provider</h3>
          </div>
          <div class="Content">
          ProviderXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX1</div>
        </div>
        <div id="Venue" class="Group">
          <div class="Label">
            <h3>Venue</h3>
          </div>
          <div class="Content">Brimble Hill School, Tadpole Lane, RedhouseBrimble Hill
          School, Tadpole Lane, RedhouseBrimble Hill 1
          <br />SN25 2NB</div>
        </div>
        <div id="Ages" class="Group">
          <div class="Label">
            <h3>Ages</h3>
          </div>
          <div class="Content">
          <span class="AgeFrom">5</span> to 
          <span class="AgeTo">16</span></div>
        </div>
        <div id="OpenTo" class="Group">
          <div class="Label">
            <h3>Open To</h3>
          </div>
          <div class="Content">All disabled young people aged 5-16, and their friends,
          siblings, relatives.</div>
        </div>
        <div id="InclusiveAccessible" class="Group">
          <div class="Label">
            <h3>Inclusive/accessible for disabled users?</h3>
          </div>
          <div class="Content">Yes</div>
        </div>
        <div id="SuitableForOlderPeople" class="Group">
          <div class="Label">
            <h3>Suitable for older people?</h3>
          </div>
          <div class="Content">No</div>
        </div>
        <div id="Cost" class="Group">
          <div class="Label">
            <h3>Cost</h3>
          </div>
          <div class="Content Free">Free unless xyzxyz</div>
        </div>
        <div id="DiscountAvailable" class="Group">
          <div class="Label">
            <h3>Discount/Leisure cards accepted?</h3>
          </div>
          <div class="Content">Free unless xyzxyz</div>
        </div>
        <div id="MoreInformation" class="Group">
          <div class="Label">
            <h3>More information</h3>
          </div>
          <div class="Content">High demand, must book in advance.</div>
        </div>
        <div id="ContactZone" class="Group">
          <div class="Label">
            <h3>Contact</h3>
          </div>
          <div id="ContactName" class="Group">
            <div class="Label">
              <h3>Contact Name</h3>
            </div>
            <div class="Content">Ben HumphreyBen HumphreyBen HumphreyBen HumphreyBen
            HumphreyBen HumphreyBen HumphreyBen HumphreyBen</div>
          </div>
          <div id="ContactTel" class="Group">
            <div class="Label">
              <h3>Contact Telephone</h3>
            </div>
            <div class="Content">01793 617782 / mobile xxxxx xxxxxx 01793 617782 01793
            617782 / mobile xxxxx xxxxxx 01793 617782 0179</div>
          </div>
          <div id="ContactEmail" class="Group">
            <div class="Label">
              <h3>Contact Email</h3>
            </div>
            <div class="Content">
              <a href="mailto:[email protected]">[email protected]</a>
            </div>
          </div>
          <div id="ContactWebsite" class="Group">
            <div class="Label">
              <h3>Contact Website</h3>
            </div>
            <div class="Content">
              <a href="http://www.ABCDEFGHIJKLMNOPQRSTUVWXYZ.gov.uk">www.ABCDEFGHIJKLMNOPQRSTUVWXYZ.gov.uk</a>
            </div>
          </div>
        </div>
        <div id="SafetyZone">
          <div class="Title">
            <h2>Safety</h2>
          </div>
          <div id="EnhancedCRB" class="Group">
            <div class="Label">
              <h3>Enhanced CRB disclosure gained</h3>
            </div>
            <div class="Content">All Staff</div>
          </div>
          <div id="ChildProtectionTrained" class="Group">
            <div class="Label">
              <h3>Child Protection trained</h3>
            </div>
            <div class="Content">Some Staff</div>
          </div>
          <div id="SafeguardingPolicy" class="Group">
            <div class="Label">
              <h3>Organisation has safeguarding policy</h3>
            </div>
            <div class="Content">Yes</div>
          </div>
          <div id="FirstAidOnSite" class="Group">
            <div class="Label">
              <h3>First Aid available on site</h3>
            </div>
            <div class="Content"></div>
          </div>
          <div id="AccreditationInsurance" class="Group">
            <div class="Label">
              <h3>Accreditation / Insurance</h3>
            </div>
            <div class="Content">Yes</div>
          </div>
          <div id="SaferRecruitment" class="Group">
            <div class="Label">
              <h3>A member of staff on each recruitment panel trained in Safer
              Recruitment</h3>
            </div>
            <div class="Content">Yes</div>
          </div>
          <div id="RegisteredWithIC" class="Group">
            <div class="Label">
              <h3>Organisation registered with the Information Commissioner</h3>
            </div>
            <div class="Content">Yes</div>
          </div>
          <div id="ExtraSafeguardingInfo" class="Group">
            <div class="Label">
              <h3>Additional Notes on Safeguarding</h3>
            </div>
            <div class="Content">more info ....................</div>
          </div>
        </div>
        <div id="ReviewSummaryZone">
          <div id="RSHeading">
            <div class="Label">
              <h3>Average rating</h3>
            </div>
            <div class="Content">
              <img src="images/1Star.gif" alt="1 Star Rating" />
            </div>
          </div>
          <div id="RSRating">
            <div class="Label">
              <h3>Number of ratings</h3>
            </div>
            <div class="Content">1 rating(s)</div>
          </div>
          <div id="RSNewReview">
            <div class="Content">
              <a href="?Type=CDA1C2A0-0BDC-47FD-B08D-BEDF8A6006DB&amp;Id=CC9488C5-C9A3-4A06-9A13-2BB9A1703157">
              Rate this</a>
            </div>
          </div>
        </div>
        <div id="ReviewZone">
          <div class="Review">
            <div class="User">
              <div class="Label">
                <h3>Name</h3>
              </div>
              <div class="Content">Gary Salter</div>
            </div>
            <div class="ReviewDate">
              <div class="Label">
                <h3>Date</h3>
              </div>
              <div class="Content">17 February 2011</div>
            </div>
            <div class="Comment">
              <div class="Label">
                <h3>Comments</h3>
              </div>
              <div class="Content">Test feedback</div>
            </div>
            <div class="Rating">
              <div class="Label">
                <h3>Rating</h3>
              </div>
              <div class="Content">
                <img src="images/1Star.gif" alt="1 Star Rating" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div> ';

I have the following long string returned from a SOAP call into a variable, and I need to find the contents of certain elements within it, namely the .MoreInfoZone .OfferZone #Heading .content and .MoreInfoZone .OfferZone #Description .content.

I can't do it with javascript, as it is for populating metatags for Facebook Open Graph, and their spiders won't run the javascript.

$soap = '    

<?xml version="1.0" encoding="utf-16"?>


    <div class="MoreInfoZone">
      <div class="OfferZone">
        <div id="Heading" class="Group">
          <div class="Label">
            <h3>Activity</h3>
          </div>
          <div class="Content">5-16 Trampoline / Swim Group 5-16 Trampoline / Swim
          Group5-16 Trampoline / Swim Group5-16 Trampolin1</div>
        </div>
        <div id="WhenZone">
          <div id="Date" class="Group">
            <div class="Label">
              <h3>
                <span class="StartDateLabel">Date</span>
              </h3>
            </div>
            <div class="Content">
            <span class="Start Date">21 December 2010</span> to 
            <span class="End Date">24 November 2011</span></div>
          </div>
          <div id="Time" class="Group">
            <div class="Label">
              <h3>Time</h3>
            </div>
            <div class="Content">
            <span class="Start Time">10:00am</span> to 
            <span class="End Time">12:00am</span></div>
          </div>
          <div id="DaysOfTheWeek" class="Group">
            <div class="Label">
              <h3>
                <span class="DaysOfTheWeekLabel">Days</span>
              </h3>
            </div>
            <div class="Content">
            <span class="weekdays">Monday, Tuesday, Wednesday, Thursday, Friday,</span> 
            <span class="weekend">Saturday, Sunday</span></div>
          </div>
        </div>
        <div id="Description" class="Group">
          <div class="Label">
            <h3>Description</h3>
          </div>
          <div class="Content">
            <p>Trampolining5-16 Trampoline / Swim Group 5-16 Trampoline / Swim Group5-16
            Trampoline / Swim Group5-16 Trampolin15-16 Trampoline / Swim Group 5-16
            Trampoline / Swim Group5-16 Trampoline / Swim Group5-16 Trampolin15-16
            Trampoline / Swim Group 5-16 Trampoline / Swim Group5-16 Trampoline / Swim
            Group5-16 Trampolin15-16 Trampoline / Swim Group 5-16 Trampoline / Swim
            Group5-16 Trampoline / Swim Group5-16 Trampolin15-16 Trampoline / Swim Group
            5-16 Trampoline / Swim Group5-16 Trampoline / Swim Group5-16 Trampolin1</p>
          </div>
        </div>
        <div id="Provider" class="Group">
          <div class="Label">
            <h3>Provider</h3>
          </div>
          <div class="Content">
          ProviderXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX1</div>
        </div>
        <div id="Venue" class="Group">
          <div class="Label">
            <h3>Venue</h3>
          </div>
          <div class="Content">Brimble Hill School, Tadpole Lane, RedhouseBrimble Hill
          School, Tadpole Lane, RedhouseBrimble Hill 1
          <br />SN25 2NB</div>
        </div>
        <div id="Ages" class="Group">
          <div class="Label">
            <h3>Ages</h3>
          </div>
          <div class="Content">
          <span class="AgeFrom">5</span> to 
          <span class="AgeTo">16</span></div>
        </div>
        <div id="OpenTo" class="Group">
          <div class="Label">
            <h3>Open To</h3>
          </div>
          <div class="Content">All disabled young people aged 5-16, and their friends,
          siblings, relatives.</div>
        </div>
        <div id="InclusiveAccessible" class="Group">
          <div class="Label">
            <h3>Inclusive/accessible for disabled users?</h3>
          </div>
          <div class="Content">Yes</div>
        </div>
        <div id="SuitableForOlderPeople" class="Group">
          <div class="Label">
            <h3>Suitable for older people?</h3>
          </div>
          <div class="Content">No</div>
        </div>
        <div id="Cost" class="Group">
          <div class="Label">
            <h3>Cost</h3>
          </div>
          <div class="Content Free">Free unless xyzxyz</div>
        </div>
        <div id="DiscountAvailable" class="Group">
          <div class="Label">
            <h3>Discount/Leisure cards accepted?</h3>
          </div>
          <div class="Content">Free unless xyzxyz</div>
        </div>
        <div id="MoreInformation" class="Group">
          <div class="Label">
            <h3>More information</h3>
          </div>
          <div class="Content">High demand, must book in advance.</div>
        </div>
        <div id="ContactZone" class="Group">
          <div class="Label">
            <h3>Contact</h3>
          </div>
          <div id="ContactName" class="Group">
            <div class="Label">
              <h3>Contact Name</h3>
            </div>
            <div class="Content">Ben HumphreyBen HumphreyBen HumphreyBen HumphreyBen
            HumphreyBen HumphreyBen HumphreyBen HumphreyBen</div>
          </div>
          <div id="ContactTel" class="Group">
            <div class="Label">
              <h3>Contact Telephone</h3>
            </div>
            <div class="Content">01793 617782 / mobile xxxxx xxxxxx 01793 617782 01793
            617782 / mobile xxxxx xxxxxx 01793 617782 0179</div>
          </div>
          <div id="ContactEmail" class="Group">
            <div class="Label">
              <h3>Contact Email</h3>
            </div>
            <div class="Content">
              <a href="mailto:[email protected]">[email protected]</a>
            </div>
          </div>
          <div id="ContactWebsite" class="Group">
            <div class="Label">
              <h3>Contact Website</h3>
            </div>
            <div class="Content">
              <a href="http://www.ABCDEFGHIJKLMNOPQRSTUVWXYZ.gov.uk">www.ABCDEFGHIJKLMNOPQRSTUVWXYZ.gov.uk</a>
            </div>
          </div>
        </div>
        <div id="SafetyZone">
          <div class="Title">
            <h2>Safety</h2>
          </div>
          <div id="EnhancedCRB" class="Group">
            <div class="Label">
              <h3>Enhanced CRB disclosure gained</h3>
            </div>
            <div class="Content">All Staff</div>
          </div>
          <div id="ChildProtectionTrained" class="Group">
            <div class="Label">
              <h3>Child Protection trained</h3>
            </div>
            <div class="Content">Some Staff</div>
          </div>
          <div id="SafeguardingPolicy" class="Group">
            <div class="Label">
              <h3>Organisation has safeguarding policy</h3>
            </div>
            <div class="Content">Yes</div>
          </div>
          <div id="FirstAidOnSite" class="Group">
            <div class="Label">
              <h3>First Aid available on site</h3>
            </div>
            <div class="Content"></div>
          </div>
          <div id="AccreditationInsurance" class="Group">
            <div class="Label">
              <h3>Accreditation / Insurance</h3>
            </div>
            <div class="Content">Yes</div>
          </div>
          <div id="SaferRecruitment" class="Group">
            <div class="Label">
              <h3>A member of staff on each recruitment panel trained in Safer
              Recruitment</h3>
            </div>
            <div class="Content">Yes</div>
          </div>
          <div id="RegisteredWithIC" class="Group">
            <div class="Label">
              <h3>Organisation registered with the Information Commissioner</h3>
            </div>
            <div class="Content">Yes</div>
          </div>
          <div id="ExtraSafeguardingInfo" class="Group">
            <div class="Label">
              <h3>Additional Notes on Safeguarding</h3>
            </div>
            <div class="Content">more info ....................</div>
          </div>
        </div>
        <div id="ReviewSummaryZone">
          <div id="RSHeading">
            <div class="Label">
              <h3>Average rating</h3>
            </div>
            <div class="Content">
              <img src="images/1Star.gif" alt="1 Star Rating" />
            </div>
          </div>
          <div id="RSRating">
            <div class="Label">
              <h3>Number of ratings</h3>
            </div>
            <div class="Content">1 rating(s)</div>
          </div>
          <div id="RSNewReview">
            <div class="Content">
              <a href="?Type=CDA1C2A0-0BDC-47FD-B08D-BEDF8A6006DB&Id=CC9488C5-C9A3-4A06-9A13-2BB9A1703157">
              Rate this</a>
            </div>
          </div>
        </div>
        <div id="ReviewZone">
          <div class="Review">
            <div class="User">
              <div class="Label">
                <h3>Name</h3>
              </div>
              <div class="Content">Gary Salter</div>
            </div>
            <div class="ReviewDate">
              <div class="Label">
                <h3>Date</h3>
              </div>
              <div class="Content">17 February 2011</div>
            </div>
            <div class="Comment">
              <div class="Label">
                <h3>Comments</h3>
              </div>
              <div class="Content">Test feedback</div>
            </div>
            <div class="Rating">
              <div class="Label">
                <h3>Rating</h3>
              </div>
              <div class="Content">
                <img src="images/1Star.gif" alt="1 Star Rating" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div> ';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我家小可爱 2024-11-15 12:45:37

请考虑:

http://code.google.com/p/phpquery/

http://php.net/manual/en/class.domxpath.php

一个 XPath 示例可能是:

<?php
$dom = new DOMDocument();
$dom->loadXml($soap);

$xpath = new DOMXPath($dom);

// .MoreInfoZone .OfferZone #Heading .content
$headingContent = $xpath->query('//*[@id="Heading"]//*[contains(@class, "Content")]')->item(0);
if ($headingContent instanceof DOMNode) {
    echo $headingContent->nodeValue;
}

// .MoreInfoZone .OfferZone #Description .content
$descriptionContent = $xpath->query('//*[@id="Description"]//*[contains(@class, "Content")]/p')->item(0);
if ($descriptionContent instanceof DOMNode) {
    echo $descriptionContent->nodeValue;
}

因为我通常只用 xpath 做这样的事情,所以我无法提供 phpquery 的工作示例。对此感到抱歉。

Consider:

http://code.google.com/p/phpquery/

or

http://php.net/manual/en/class.domxpath.php

An XPath example could be:

<?php
$dom = new DOMDocument();
$dom->loadXml($soap);

$xpath = new DOMXPath($dom);

// .MoreInfoZone .OfferZone #Heading .content
$headingContent = $xpath->query('//*[@id="Heading"]//*[contains(@class, "Content")]')->item(0);
if ($headingContent instanceof DOMNode) {
    echo $headingContent->nodeValue;
}

// .MoreInfoZone .OfferZone #Description .content
$descriptionContent = $xpath->query('//*[@id="Description"]//*[contains(@class, "Content")]/p')->item(0);
if ($descriptionContent instanceof DOMNode) {
    echo $descriptionContent->nodeValue;
}

as I normally only do stuff like this with xpath, I can't provide a working example with phpquery. Sorry about that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文