December 21, 201510 yr Hello Webdesignerforum.. I'm currently working on extracting specific parts from a different website. My plan is to get the html source code and extract the part with preg_match I need. This is what i currently have. $str = htmlspecialchars(file_get_contents('http://www.example.com/')); $start = '<title>'; $end = '</title>'; preg_match('/'.$start.'(.*?)'.$end.'/', $str, $match); echo $match[1]; My search strings $start and $end are both part of the source code and can consist of all the normal symbols you might find in a html code like ' " , < > / : ; so i guess i have to avoid these symbols as much as possible. If i search like this: $str = htmlspecialchars(file_get_contents('http://www.example.com/')); $start = 'title'; $end = 'title'; preg_match('/'.$start.'(.*?)'.$end.'/', $str, $match); echo $match[1]; this returns: >Example Domain</ But when i add </> i get into trouble. I'm not an expert with preg_match and I don't know if this is the best way to grab the data. Hope you can help me! Any comments can be a big help! Edited December 21, 201510 yr by TobiasKnudsen
December 22, 201510 yr Have you read the documentation on Preg_Match? Some of the examples look similar to what you are looking for http://php.net/manual/en/function.preg-match.php
December 23, 201510 yr `$str = htmlspecialchars(file_get_contents('http://www.example.com/'));` This line here is your culprit. You're running the whole page content through htmlspecialchars which converts <>/ to html entities and your regex can't find an exact match. Either change those symbols in html entities counterpart or remove htmlspecialchars function.
December 24, 201510 yr Author I found some str commands and got it to work with my database. Here is my code, if anyone has the same problem and could use my solution. Thanks for your help. // Database Connection require_once('Connections/DatabaseCon.php'); //Get the html code from the link in my databse $html = file_get_contents($row_Recordset1['url']); //Then I remove all new lines because this gave me problems with the search later. $var = str_replace(array("\r","\n"),"",$html); //Then I define my 2 search variables from my database $tag_begin = $row_Recordset1['search1']; $tag_end = $row_Recordset1['search2']; $begin = strpos($var, $tag_begin)+strlen($tag_begin); $end = strpos($var, $tag_end); $length = $end-$begin; $result = substr($var, $begin, $length); echo $result; This works when the search strings are unique on the html page or the first match Hope this can help some of you! Good Luck
Create an account or sign in to comment