-1

I'm trying to extract HTML code from a website.

First, I insert the HTML into a .txt file. Then, another page fopens the txt file while str_replacing selected regex.

I keep getting resource ID #4 on this below script. I don't know why information is not passing through. I'm rusty.

I want to trim the selected data from the .txt file and insert it into another .txt file.

<?php
set_time_limit(0);

error_reporting(E_ALL);

$fp = fopen('newfile2.txt', 'r');
echo '$fp is resource = ' . (is_resource($fp) ? 'true': 'false');

$re = '/(\n*.*+)+\1<tr>\n.*<td bgcolor=\"#ffffcc\">/'; 
$subst = "<tr><td>"; 

$result = str_replace( $re, $subst, $fp);

$put = file_put_contents("newfile3.txt", $result);

print_r($result);
echo 'testjn';

?>
mjk
  • 2,443
  • 4
  • 33
  • 33
  • the same with preg_replace? – finessing Feb 06 '16 at 00:46
  • i have some code for later require('simple_html_dom.php'); $table = array(); $html = file_get_html('newfile2.txt'); foreach($html->find('tr') as $row) { $calls = $row->find('td',0)->plaintext; $clast = $row->find('td',1)->plaintext; – finessing Feb 06 '16 at 00:52
  • though, its a huge html file, and collects too much. im just trying to trim the code up to use this script later – finessing Feb 06 '16 at 00:52
  • I've posted an answer below that should work for you, presuming that regex works. Take a look at that. I dont know what you are trying to parse so re-writing that regex or showing a parser example would be pretty hard. – chris85 Feb 06 '16 at 00:54
  • Any questions or issues with answer? http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – chris85 Feb 06 '16 at 01:06

1 Answers1

1

fopen returns a resource, not a string. You will need to use fread with that or file_get_contents.

Additionally str_replace doesn't work with regular expressions. You can use preg_replace for that.

$fp = file_get_contents('newfile2.txt');
$re = '/(\n*.*+)+\1<tr>\n.*<td bgcolor=\"#ffffcc\">/'; 
$subst = "<tr><td>"; 
$result = preg_replace( $re, $subst, $fp);
$put = file_put_contents("newfile3.txt", $result);
print_r($result);
echo 'testing';

You should look into using a parser for HTML/XML as well. How do you parse and process HTML/XML in PHP?

Here's how your regex currently works, https://regex101.com/r/gD0wP7/1. This question didn't seem to be about the regex though.

Community
  • 1
  • 1
chris85
  • 23,846
  • 7
  • 34
  • 51