Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    fopen problem with str_replace

    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>
    now...
    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 -

    however,
    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
    <b-class=titler>Getting-to-the-Root-of-Nutrition:-The-Pillars-of-Nutrition</b><b-class=titler>Getting-to-the-Root-of-Nutrition:-The-Pillars-of-Nutrition</b>
    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 )
    PHP Code:
    if (preg_match("/(<b class=titler>)(.*)(<\/b>?)/"$h->body$title))
                
    $aTitle htmlentities($title[0], ENT_QUOTES);
            else
                
    $aTitle "ERROR"
    My fopen bit of code ( $template is stored on another included page )
    PHP Code:
    $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
                
    }
            } 

  • #2
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Umm...
    Code:
    $newtitler = str_replace('<b class=titler>','', $aTitle);
    $newtitler .= str_replace('</b>','', $aTitle);
    $newtitle = $newtitler;
    Given that code:
    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:
    PHP Code:
    $newTitle str_replace("<b class=titler>"""$aTitle);
    $newTitle str_replace("</b>"""$newTitle); 
    Or regexp:
    PHP Code:
    $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.
    Last edited by Mhtml; 01-29-2007 at 02:22 PM.
    Omnis mico antequam dominus Spookster!

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi,

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

    thanks for help so far

  • #4
    New Coder
    Join Date
    Aug 2006
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    think may have cracked it .. or at least found a temporary solution
    PHP Code:
    if (preg_match("/(<b class=titler>)(.*)(<\/b>?)/"$h->body$title)) 
                
    $aTitle htmlentities($title[0], ENT_QUOTES); 
            else 
                
    $aTitle "ERROR 
    I just removed htmlentities from the above code and am testing it now

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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:

    PHP Code:
    <?php

    // 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);

    }
    ?>

  • #6
    New Coder
    Join Date
    Aug 2006
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    if (preg_match("/(<b class=titler>)(.*)(<\/b>?)/"$h->body$title)) 
                
    $aTitle htmlentities($title[0], ENT_QUOTES); 
            else 
                
    $aTitle "ERROR"
    Thanks for help so far .. it really is appreciated

  • #7
    New Coder
    Join Date
    Aug 2006
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks mlseim .. i'll have a scout now see what bits i can use and let you know

  • #8
    New Coder
    Join Date
    Aug 2006
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    mlseim..

    working like a charm !

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

    many thanks both of you !

  • #9
    New Coder
    Join Date
    Aug 2006
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    got it working great !

    many thanks guys

  • #10
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.
    Omnis mico antequam dominus Spookster!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •