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.


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

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){
foreach($file_ids as $file_id=>$id) {
$input = file_get_contents($id);

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

Thanks for any suggestions


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


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”.