SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2003
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Content Grabbing & Regular Expression

    Hi,

    I am trying to grab certain content from a website so I can store it in MySQL, however I am having some problems here can you please take a look at my codes and see where I am doing wrong?

    PHP Code:
    <?php 
    $fp 
    fopen("http://www.astrology-online.com/daily.htm""r");
    $content fread($fp100000);
    fclose($fp);

    preg_match_all("[<!-- CHANGE ARIES HERE -->.*<!-- END ARIES HERE -->]"$content$matches);
    for (
    $i 0$i <= 11$i++) {
        print(
    $matches[0][$i] . "<p>");
    }
    ?>
    I am sorry if the codes look dumb but I am really new to regular expression so I don't really know how to make use of it.

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The text problably goes over several lines and therefore you need to use the "s" switch for the regexp.

    Try:

    Code:
    <pre>
    <?php
    
    $text = "asdasdasdadsasdadsasd
      <!-- CHANGE ARIES HERE -->
    A new you could help your attitude. 
    The emotional state of peers may cause a problem for you. 
    Discrimination will be in order. 
    
    <!-- END ARIES HERE -->
    asdasdsad";
    
    preg_match('#<!-- CHANGE ARIES HERE -->(.+?)<!-- END ARIES HERE -->#is', $text, $match);
    
    print_r($match);
    
    ?>
    </pre>

  3. #3
    SitePoint Member
    Join Date
    Apr 2003
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Chris82, you almost slove all my problem it's a great help! However I have some questions again :P

    1. What is the use for "#" in regexp?
    2. Why use a "(.+?)" to wrap the code ".+?"?
    3. What does "#is" mean?
    4. Where can read more about regexp for PHP?

    This is the code I made changes from your example, however it seems something is wrong with print() causing it not to display array or I am using the wrong functions.

    PHP Code:
    <?php 
    $fp 
    fopen("http://www.astrology-online.com/daily.htm""r");
    $content fread($fp100000);
    fclose($fp);

    preg_match_all('#<!-- CHANGE \w{3,11} HERE -->(.+?)<!-- END \w{3,11} HERE -->#is'$content$out);
    print_r($out);
    for(
    $i 1$i <= 12$i++) {
        print(
    $out[$i]);
    }
    ?>
    Quote Originally Posted by From PHP.NET
    A temporary note, until the documentation is changed: print can NOT be used in variable functions; see http://bugs.php.net/bug.php?id=16739
    Quote Originally Posted by Output from the above code
    Array ( [0] => Array ( [0] => A new you could help your attitude. The emotional state of peers may cause a problem for you. Discrimination will be in order. [1] => Someone you live with will, be impossible. You can make excellent career moves if you are open to the opportunities that exist. Problems with colleagues are likely. [2] => Outbursts of passion may cause arguments with your mate. Your lover will cost you dearly if you let them. Don't overdo it. [3] => You can gain approval, get kudos, and ask for help if you put a little heart into your speech or request. Don't let the moods of those you live with get you down. Travel and entertaining conversation will be informative and uplifting. [4] => You can count on getting a pat on the back for your efforts. You may find that others do not do things the way you want; however, if the job gets done, let it pass. Children or friends may try to talk you into lending them money. [5] => Use your ingenuity to manipulate things to get what you want. Talk to those in a position of power about your intentions. Show what a dedicated person you can be. [6] => You'll have amazing ideas, but superiors may try to block your attempts at implementing them. Travel will enhance romance and adventure. Others may want to steal your thunder when they realize your ideas are pretty solid. [7] => You may find that you are a little lucky today. Reevaluate your position and make decisions about your future goals. Don't cause a scene, but when you get home let your partner know how you feel and why. [8] => Trying to deal with your mate will be unproductive and possibly hurtful. You will impress others with your initiative and ability to accomplish while on short business trips. Pamper yourself; you deserve it. [9] => You could overreact to emotional situations regarding your relationship. Trips, educational pursuits, or projects that could lead to new outlooks should be on your agenda. Opportunities to get together with people in powerful positions could help you get ahead. [10] => You will be able to borrow money in order to invest. You can get into self awareness groups or look into physical enhancement programs. Romance can surface if you get into some of those fitness programs you've been putting off. [11] => Your mind will be on matters that deal with secret affairs. You may be overreacting to a situation at hand. Get busy trying to make more money. ) [1] => Array ( [0] => A new you could help your attitude. The emotional state of peers may cause a problem for you. Discrimination will be in order. [1] => Someone you live with will, be impossible. You can make excellent career moves if you are open to the opportunities that exist. Problems with colleagues are likely. [2] => Outbursts of passion may cause arguments with your mate. Your lover will cost you dearly if you let them. Don't overdo it. [3] => You can gain approval, get kudos, and ask for help if you put a little heart into your speech or request. Don't let the moods of those you live with get you down. Travel and entertaining conversation will be informative and uplifting. [4] => You can count on getting a pat on the back for your efforts. You may find that others do not do things the way you want; however, if the job gets done, let it pass. Children or friends may try to talk you into lending them money. [5] => Use your ingenuity to manipulate things to get what you want. Talk to those in a position of power about your intentions. Show what a dedicated person you can be. [6] => You'll have amazing ideas, but superiors may try to block your attempts at implementing them. Travel will enhance romance and adventure. Others may want to steal your thunder when they realize your ideas are pretty solid. [7] => You may find that you are a little lucky today. Reevaluate your position and make decisions about your future goals. Don't cause a scene, but when you get home let your partner know how you feel and why. [8] => Trying to deal with your mate will be unproductive and possibly hurtful. You will impress others with your initiative and ability to accomplish while on short business trips. Pamper yourself; you deserve it. [9] => You could overreact to emotional situations regarding your relationship. Trips, educational pursuits, or projects that could lead to new outlooks should be on your agenda. Opportunities to get together with people in powerful positions could help you get ahead. [10] => You will be able to borrow money in order to invest. You can get into self awareness groups or look into physical enhancement programs. Romance can surface if you get into some of those fitness programs you've been putting off. [11] => Your mind will be on matters that deal with secret affairs. You may be overreacting to a situation at hand. Get busy trying to make more money. ) ) Array
    Notice: Undefined offset: 2 in C:\httpd\www\private\devel\horoscope\TMPmicsecu7zt.php on line 16

    Notice: Undefined offset: 3 in C:\httpd\www\private\devel\horoscope\TMPmicsecu7zt.php on line 16

    Notice: Undefined offset: 4 in C:\httpd\www\private\devel\horoscope\TMPmicsecu7zt.php on line 16

    Notice: Undefined offset: 5 in C:\httpd\www\private\devel\horoscope\TMPmicsecu7zt.php on line 16

    Notice: Undefined offset: 6 in C:\httpd\www\private\devel\horoscope\TMPmicsecu7zt.php on line 16

    Notice: Undefined offset: 7 in C:\httpd\www\private\devel\horoscope\TMPmicsecu7zt.php on line 16

    Notice: Undefined offset: 8 in C:\httpd\www\private\devel\horoscope\TMPmicsecu7zt.php on line 16

    Notice: Undefined offset: 9 in C:\httpd\www\private\devel\horoscope\TMPmicsecu7zt.php on line 16

    Notice: Undefined offset: 10 in C:\httpd\www\private\devel\horoscope\TMPmicsecu7zt.php on line 16

    Notice: Undefined offset: 11 in C:\httpd\www\private\devel\horoscope\TMPmicsecu7zt.php on line 16

    Notice: Undefined offset: 12 in C:\httpd\www\private\devel\horoscope\TMPmicsecu7zt.php on line 16
    Seems like I have successfully grabbed all contents and place them in a array, but problem is how to display them to the browser.

    Any ideas from anyone?

  4. #4
    SitePoint Member
    Join Date
    Apr 2003
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    EDIT: Nevermind I got it working.
    Last edited by Draculax; Apr 4, 2003 at 18:08.


Bookmarks

Posting Permissions

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