ari_aaron Posted February 1, 2007 Share Posted February 1, 2007 I think I need to use Regular Expressions here, but I'm not sure how: I need to read a file, and get what's in the span with the ID of "name". For example, if it had <span id="name">WHAT'S HERE??</span> i would want "WHAT'S HERE??" Thanks, ari_aaron Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 1, 2007 Share Posted February 1, 2007 <?php $var = "<span id='name'>WHAT'S HERE??</span>"; $new_var = substr("$var", 16, 13); echo $new_var; ?> This would only work for that specific span id of course...I'm not sure of how you could detect the start and end of the tag...but if your looking for a span with an 'id' conating 4 letters such as name, that should work. <b>Edit:</b> I was wrong...you might have to edit the "13" depending on how long the tag is in between. I will see if I can come up with a better way to answer your question. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 1, 2007 Share Posted February 1, 2007 This seems to work perfect ^^ <?php $var = "<span id='name'>test test test test</span>"; $new_var = explode('>' , $var); echo $new_var[1]; ?> $new_var[1] holds what is inside of the tag. Quote Link to comment Share on other sites More sharing options...
ari_aaron Posted February 2, 2007 Author Share Posted February 2, 2007 I need to read a file, and get what's in the span with the ID of "name". I'm using $contents = file_get_contents($url); and I need to find the thing in the tag, which is somewhere in the page. Quote Link to comment Share on other sites More sharing options...
fert Posted February 2, 2007 Share Posted February 2, 2007 preg_match_all("/<span id='name'>(.*?)<\/span>/",$contents,$matches); print_r($matches) Quote Link to comment Share on other sites More sharing options...
ari_aaron Posted February 2, 2007 Author Share Posted February 2, 2007 Output: Array ( [0] => Array ( [0] => WHAT'S HERE?? ) [1] => Array ( [0] => WHAT'S HERE?? ) ) How do I get access to it without all that array stuff. I tried $matches[0], and it outputted "Array". EDIT: got it. it was $matches[0][0] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.