PHP str_replace not passing data -


i'm trying extract html code website.

first, insert html .txt file. then, page fopens txt file while str_replacing selected regex.

i keep getting resource id #4 on below script. don't know why information not passing through. i'm rusty.

i want trim selected data .txt file , insert .txt file.

<?php set_time_limit(0);  error_reporting(e_all);  $fp = fopen('newfile2.txt', 'r'); echo '$fp 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';  ?> 

fopen returns resource, not string. need use fread or file_get_contents.

additionally str_replace doesn't work regular expressions. can use preg_replace 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 using parser html/xml well. how parse , process html/xml in php?

here's how regex works, https://regex101.com/r/gd0wp7/1. question didn't seem regex though.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -