Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Advanced search in HTML string. (Preg_match)

Featured Replies

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 by TobiasKnudsen

`$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.

  • 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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.