Raz0r Posted November 17, 2009 Share Posted November 17, 2009 I am wondering, is it possible to get HTML elements with PHP.... Let's say, that some page has this HTML code: <h1>This is my website</h1>. Is it possible, that PHP check if that line exists in HTML code? Quote Link to comment https://forums.phpfreaks.com/topic/181880-getting-html-elements-with-php/ Share on other sites More sharing options...
Alex Posted November 17, 2009 Share Posted November 17, 2009 Yes it can be done. Depending on if you know exactly what you're searching for or not you might need to use PCRE functions, but for your specific example you could use strpos(). Example: if(strpos($content, '<h1>This is my website</h1>') !== false) { echo 'Found!' } Quote Link to comment https://forums.phpfreaks.com/topic/181880-getting-html-elements-with-php/#findComment-959230 Share on other sites More sharing options...
siric Posted November 17, 2009 Share Posted November 17, 2009 I am wondering, is it possible to get HTML elements with PHP.... Let's say, that some page has this HTML code: <h1>This is my website</h1>. Is it possible, that PHP check if that line exists in HTML code? You can use substr_count(string,substring,start,length) Check http://www.w3schools.com/PHP/php_ref_string.asp Quote Link to comment https://forums.phpfreaks.com/topic/181880-getting-html-elements-with-php/#findComment-959231 Share on other sites More sharing options...
cags Posted November 17, 2009 Share Posted November 17, 2009 As AlexWD says using strpos would certainly be good enough if you simply wish to test for the string. If however you plan on doing any 'proper' scraping you'd probably be better off with something like domdocument or simplexml rather than Regular Expressions (or possibly a combination of the two). Quote Link to comment https://forums.phpfreaks.com/topic/181880-getting-html-elements-with-php/#findComment-959233 Share on other sites More sharing options...
Raz0r Posted November 17, 2009 Author Share Posted November 17, 2009 Yes it can be done. Depending on if you know exactly what you're searching for or not you might need to use PCRE functions, but for your specific example you could use strpos(). Example: if(strpos($content, '<h1>This is my website</h1>') !== false) { echo 'Found!' } Hmm.... You didn't understand me well., i think In your code, where is variable of website that will be checked for that code in HTML? Quote Link to comment https://forums.phpfreaks.com/topic/181880-getting-html-elements-with-php/#findComment-959239 Share on other sites More sharing options...
premiso Posted November 17, 2009 Share Posted November 17, 2009 Hmm.... You didn't understand me well., i think In your code, where is variable of website that will be checked for that code in HTML? I think he explained it well for how you explained it. If that is not right, post 2 real examples that you would use so we can better understand what you want to be achieved. Given the original topic, Alex answered you question perfectly. Doing a better explanation with examples of what you want exactly will help you get the answer you want. Quote Link to comment https://forums.phpfreaks.com/topic/181880-getting-html-elements-with-php/#findComment-959244 Share on other sites More sharing options...
Raz0r Posted November 17, 2009 Author Share Posted November 17, 2009 Ok.... I want a PHP that will check if some code exists in HTML code of some website. Let's say i want to check Example.com... I need a PHP code in wich I will write some HTML code(let's say: <h1>website</h1>), and in that PHP code i will write website wich I want to check(in this case: Example.com), and that PHP code will look in Example.com website, and tell me if <h1>website</h1> code exists in their HTML.... Quote Link to comment https://forums.phpfreaks.com/topic/181880-getting-html-elements-with-php/#findComment-959248 Share on other sites More sharing options...
premiso Posted November 17, 2009 Share Posted November 17, 2009 You will need to first grab the contents of the website, curl or file_get_contents will be your primary source to figure that out. Second use the method Alex provided to search the retrieved string data for that string you want and viola, you have it. Quote Link to comment https://forums.phpfreaks.com/topic/181880-getting-html-elements-with-php/#findComment-959265 Share on other sites More sharing options...
cags Posted November 17, 2009 Share Posted November 17, 2009 First you will have to fetch the contents of the website in quesion. You can do this using something like cURL or file_get_contents. You can then use the examples given. In it's most basic form... $url = 'http://www.google.com'; $search = '<h1>bob</h1>'; $html = file_get_contents($url); if(strpos($html, $search) !== FALSE) { echo 'Found'; } Faux Edit: premiso beat me to it, but figured I'd post anyway as I had a basic example. Quote Link to comment https://forums.phpfreaks.com/topic/181880-getting-html-elements-with-php/#findComment-959267 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.