开发者

How to locate the ID tag of a string and grab its value

Due to the nature of Drupal Calendar's confusing web of functions I'm going to have to do something different.

I need to grab the id tag and its value from an array containing html code.

<div class="view-item view-item-calendar" id="node-154">
  <div class="calendar.111.field_showtimes_two.0.0 calendar monthview mainstagetheatre-highlight">
              <div class="view-field view-data-node-title node-title">


        <a href="/tca/mainstage/ninetofivemusical">9 to 5: The Musical</a>      </d开发者_如何学Goiv>  
      </div>    
</div>

This is the code I will need to parse. Is it possible to target that id tag and whatever I set it as? Im doing it this way because I don't know where the array is created so I can add a node id, so I have to add it where the html is created.


ariel is right. Use an (X)HTML parser. This should work:

$dom = new DOMDocument;
$dom->loadHTML($html_from_above);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//@id') as $node) {
   echo $node->value //found!
}

Alternatively, since you have only one single id, you might just want to use regex anyway. Better yet, use strpos/substr.


You need an HTML parser. Don't try to do it using regular expression.

To choose an HTML parser refer to this topic Robust and Mature HTML Parser for PHP

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜