Returning HTML element id with hyphens

I have a piece of code that returns all of the ids from an html page except when the id has hyphens on the name. Of course that is the specific id that I need to be returned. Here is the html code that I’m targeting.

 <h1>

        <span class="" id="parent-fieldname-title">
            Lecture 23: Energy, CMOS
        </span>
    </h1>

I need to pull the text between the span with the id parent-fieldname-title out and use it as a title.
Here is the php block I have to pull ids.

//retrieve page ids
function get_ids($file_ids){
 natsort($file_ids);
foreach($file_ids as $file_id=>$id) {
$input = file_get_contents($id);
   preg_match_all('/(id="(\\w*)")/is',$input,$patterns);

   if(!empty($patterns[0])){
        echo "<br/>ID's:<ul>";
        foreach($patterns[0] as $key => $val){
        $val = preg_replace("/</","<",$val);
            echo "<li>" . htmlentities($val) . "</li>";
        }
        echo "</ul>";
    }else{
        echo "<br/>No ID's found<br/>";
    }
}
return $val;
}
$file_ids = get_ids(glob('f:/wamp/www/courses/contents/video-lectures/lecture-*/index.htm'));
//print_r($file_ids);

Thanks for any suggestions

preg_match_all(‘/(id=“(\w*)”)/is’,$input,$patterns);

only looks for alphanumeric characters. If you want hyphens as well you need to change that line to:

preg_match_all(‘/(id=“([\w-]*)”)/is’,$input,$patterns);

Excellent! Thanks much felgall.

I don’t suppose you could point me to a function that would let me pull the text from between the span elements that have that particular id?

Tsalagi, your best (and easiest, in the long term) option is to use a tool dedicated to making sense out of reading, traversing and manipulating HTML; like the DOM.

With that, you can search and retrieve parts of the HTML documents based on any criteria that you have, such as “give me the text contents of any <span>s which have an id associated with them”.