View Full Version : fopen problem with str_replace

01-29-2007, 12:05 PM
Hi guys ok my problem is this..

i have a page that scrapes content from another site
it searches the remote site using pregmatch to find the code from where it should start copying from

however when i get the code back its like
<b class=titler>Wine Tastings for Singles</b><b class=titler>Wine Tastings for Singles</b>
using str_replace i have the following code

$newtitler = str_replace('<b class=titler>','', $aTitle);
$newtitler .= str_replace('</b>','', $aTitle);
$newtitle = $newtitler;

$aTitle is the variable that the script defines for the scraped title

would seem easy enough .. you'd assume that $newtitle would now be
Wine Tastings for Singles

I then use str_replace again to replace the spaces with a -

when i use fopen i get this
Warning: fclose(): supplied argument is not a valid stream resource in /home/pettrain/public_html/articles/getContent.php on line 184
Warning: fopen(&lt;b-class=titler&gt;Getting-to-the-Root-of-Nutrition:-The-Pillars-of-Nutrition&lt;/b&gt;&lt;b-class=titler&gt;Getting-to-the-Root-of-Nutrition:-The-Pillars-of-Nutrition&lt;/b&gt;&lt;b-class=titler&gt;Getting-to-the-Root-of-Nutrition:-The-Pillars-of-Nutrition&lt;/b&gt;&lt;b-class=titler&gt;Getting-to-the-Root-of-Nutrition:-The-Pillars-of-Nutrition&lt;/b&gt;.php) [function.fopen]: failed to open stream: No such file or directory

So.. it hasnt removed the bit of code using str_replace
i wanted it removed.. instead i get the above

anyone help please .. its very urgent

Thanks in advance

I have tried different ways etc but nothing will work
If you could sort it so that it doesnt include the <b class > bit of html in the following code snippet that would solve all my problems

Here is the code bits

Content scraping bit ( there is a lot more but this is what grabs & stores the content )

if (preg_match("/(<b class=titler>)(.*)(<\/b>?)/", $h->body, $title))
$aTitle = htmlentities($title[0], ENT_QUOTES);
$aTitle = "ERROR";

My fopen bit of code ( $template is stored on another included page )

$newtitler = str_replace('<b class=titler>','', $aTitle);
$newtitler .= str_replace('</b>','', $aTitle);
$newtitle = $newtitler;
$newfilename = str_replace(' ','-', $newtitle);
$newtitler2 = str_replace('<b-class=titler>','', $newfilename);
$newtitler2 .= str_replace('</b>','', $newfilename);
$newtitle2 = $newtitler2;
$newfilename2 = str_replace(' ','-', $newtitle2);
$ext = '.php';
echo $newfilename;
$filename = $newfilename2; //the name of our file.
$filename .= $ext;
$filecontent = $template; //what we will be writing to our file.
$strlength = strlen($filecontent); //gets the length of our $content string.
$create = fopen($filename, "w"); //uses fopen to create our file.
$write = fwrite($create, $filecontent, $strlength); //writes our string to our file.
$close = fclose($create); //closes our file

01-29-2007, 02:17 PM

$newtitler = str_replace('<b class=titler>','', $aTitle);
$newtitler .= str_replace('</b>','', $aTitle);
$newtitle = $newtitler;

Given that code:

$aTitle = "<b class=titler>text</b>";
$newtitler = str_replace("<b class=titler>", "", $aTitle);//$newtitler is now: text</b>
$newtitler .= str_replace("</b>", "", $aTitle);//$newtitler is now: text</b><b class=titler>text

So you could do:

$newTitle = str_replace("<b class=titler>", "", $aTitle);
$newTitle = str_replace("</b>", "", $newTitle);
Or regexp:

$newTitle = preg_replace("/\<b class=titler\>(.*)\<\b\>/", "\\$1", $aTitle);

I hope I read that right, it's like 1:30am here I'm just on my way to bed so I hope I didn't just mess up there. Night.

01-29-2007, 07:32 PM

have tried both methods and still same results
could i email you the files with the line numbers ?

thanks for help so far

01-29-2007, 07:45 PM
think may have cracked it .. or at least found a temporary solution

if (preg_match("/(<b class=titler>)(.*)(<\/b>?)/", $h->body, $title))
$aTitle = htmlentities($title[0], ENT_QUOTES);
$aTitle = "ERROR

I just removed htmlentities from the above code and am testing it now

01-29-2007, 07:50 PM
I don't know if this will help or not ...

Below is an edited script of an RSS feeder, where it opens a website,
searches for a section, and then picks out parts within the section.

You might get some ideas, especially about reading a whole web page:


// Get page ... this opens the web page and reads all lines of code
$url = "http://www.mywebsite.net/index.html";
$data = implode("", file($url));

// Get content items between <div class="feature"> and </div>
preg_match_all ("/<div class=\"feature\">([^`]*?)<\/div>/", $data, $matches);

// Loop through each item
foreach ($matches[0] as $match) {

// Get title ... which is between <h3> and </h3>
preg_match ("/<h3>([^`]*?)<\/h3>/", $match, $temp);
$title = $temp['1'];
$title = strip_tags($title);
$title = trim($title);

// Get date ... which is between <h4> and </h4>
preg_match ("/<h4>([^`]*?)<\/h4>/", $match, $temp);
$date = $temp['1'];
$date = trim($date);

// Get text ... which is between <p> and </p>
preg_match ("/<p>([^`]*?)<\/p>/", $match, $temp);
$text = $temp['1'];
$text = trim($text);


01-29-2007, 07:55 PM
nope still same..

however .. its now <b-class="titler">

im close to pulling hair out lol

lets forget the fopen bit for now..

using the below preg match code..
is there anyway i can get it to search for the "code" in the page but not include it when setting the content as the variable value ?

the code for the preg match is now

if (preg_match("/(<b class=titler>)(.*)(<\/b>?)/", $h->body, $title))
$aTitle = htmlentities($title[0], ENT_QUOTES);
$aTitle = "ERROR";

Thanks for help so far .. it really is appreciated

01-29-2007, 07:56 PM
thanks mlseim .. i'll have a scout now see what bits i can use and let you know :)

01-29-2007, 08:36 PM

working like a charm !

just need to iron out a few kinks but its working great !

many thanks both of you !

01-29-2007, 09:00 PM
got it working great !

many thanks guys :)

01-30-2007, 12:54 AM
What I showed you was a recurring algorithmic error in your code. If you looked a few lines down you should notice you were making the same mistake again.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum