Jump to content

Prismatic

Members
  • Posts

    503
  • Joined

  • Last visited

Posts posted by Prismatic

  1. So here's what I'm trying to do, and I haven't found any clear tutorials on how to properly navigate a DOMDocument object, at least not in the strict sense of PHP.

     

    I'm building a web scraper, I've had it working for some time now using more traditional methods (a combination of string manipulation and clever regex). I've been told xpath can be much faster and more reliable for what I need. Sold.

     

    Let's say I'm parsing a forum. This forum separates each reply in a post with a set of <li></li> with a class of "message"

     

    <li class="message">
        // Stuff here
    </li>
    
    <li class="message">
        // Stuff here
    </li>
    

     

    So far so good. These list items contain all the formatting for each post, including user info and the message text. Each sitting in it's own div.

     

     

    <li class="message">
    <div class="user info">
    	User info here
    </div>
    
    <div class="message text">
    	Message text here	
    </div>
    </li>
    
    <li class="message">
    <div class="user info">
    	User info here
    </div>
    
    <div class="message text">
    	Message text here	
    </div>
    </li>

     

    Still with me? Good.

     

    With this bit of code I can select each message list item block and iterate over all the sub nodes inside.

     

    $items = $xpath->query("//li[starts-with(@class, 'message')]");
    
    for ($i = 0; $i < $items->length; $i++) {
       echo $items->item($i)->nodeValue . "\n";
    }

     

     

    This produces a basic text dump of the entire forum. Close, but not what I need.

     

    What I'm trying to do is as follows

     

    • Select all the class="message" list items [done]
    • Once those have been selected, run another $xpath->query to select the child nodes which contain the user info and message text

     

    Step one is done, step two is what is confusing me. How can I run a new query based on the output from the first query?

     

    Thanks guys

  2. <?php
    
    function tab_replace($args) {
        $tmp = '[h1='.str_replace("\t",'/t', $args[0]).'][/h1]';
        return $tmp;
    }
    
    $text = '
    One tab
    	Two tabs
    		Three tabs
    ';
    
    $output = preg_replace_callback('/\t+/', 'tab_replace', $text);
    echo $output;

     

    That works great thank you :)

     

    [h1=/t][/h1]This text is inside the pre element, it will be parsed.
    [h1=/t][/h1]Tabbed text
    [h1=/s/s][/h1]Two spaces
    [h1=/s/s/s][/h1]Three spaces
    [h1=/s/s/s/s][/h1]four spaces[h1=/t][/h1]and a tab
    [h1=/t/t][/h1]Two tabs
    

     

    Adapted it for spaces 2+

     

    $output = preg_replace_callback("/[ ]{2,}/", "space_replace" , $output);
    
    ...
    
    function space_replace($args)
    {
    	$tmp = '[h1='.str_replace(" ",'/s', $args[0]).'][/h1]';
        return $tmp;
    }
    

     

     

    Cheers!

     

     

    Edit - where's the solved button at?

  3. What I'm working on is complicated but my problem isn't.

     

    I'm trying to convert sets of tabs and spaces into other characters.

     

    For example, say I have the following.

    One tab
    	Two tabs
    		Three tabs
    

     

    What I'm trying to do is end up with the following

    [hl=/t][/hl]One tab
    [hl=/t/t][/hl]Two tabs
    [hl=/t/t/t][/hl]Three tabs
    

     

    Note the three /t's for the three tabs. My issue is when the script generates the regex to do the first line there, which has one tab, the regex is

    /[\t]{1}/

     

    But that converts all the tabs. It's hard to explain ugh.

     

    All I can manage is

    [hl=/t][/hl]One tab
    [hl=/t/[/hl][hl=/t][/hl]Two tabs
    [hl=/t/][/hl][hl=/t][/hl][hl=/t][/hl]Three tabs
    

     

     

    help?

  4. <?php
    $data = file_get_contents("http://letsbetrends.com/api/current_trends");
    
    $json_data = json_decode($data, true);
    
    foreach($json_data as $branches)
    {
        foreach($branches as $entry)
        {
            if($entry['name'] == "Halloween")
            {
                print_r($entry);
            }
        }
    }
    ?>
    

     

    That will only display topics created by Halloween, for example.

  5. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <table>
            <tr>
                <td valign="top"><strong>URLs</strong><br/><textarea cols="50" rows="4" name="urls" id="urls"></textarea></td>
                <td valign="top"><strong>Keywords</strong><br/><textarea cols="50" rows="4" name="keywords" id="keywords"></textarea></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" text="Submit"></td>
            </tr>
        </table>
    </form>
    
    <?php
    
    $out = "";
    
    if($_POST['urls'] != "" && $_POST['keywords'] != "")
    {
        $keywords = explode("\n", $_POST['keywords']);
        
        $urls = explode("\n", $_POST['urls']);
        
        $out = "";
        
        for($i = 0; $i < count($urls); $i++)
        {
            $out .= "<a href='". $urls[$i] ."'>". ($keywords[$i] == "" ? $urls[$i] : $keywords[$i]) ."</a><br/>\n"; 
        }
    }
    
    echo $out;
    ?>
    

  6. As per fpdf.org

     

    Output

    string Output([string name, string dest])

     

    Description

     

    Send the document to a given destination: browser, file or string. In the case of browser, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.

    The method first calls Close() if necessary to terminate the document.

    Parameters

     

    name

    The name of the file. If not specified, the document will be sent to the browser (destination I) with the name doc.pdf.

    dest

    Destination where to send the document. It can take one of the following values:

    I: send the file inline to the browser. The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.

    D: send to the browser and force a file download with the name given by name.

    F: save to a local file with the name given by name (may include a path).

    S: return the document as a string. name is ignored.

  7. <?php
    // Populate $people with two arrays, each containing two entries.
    // "name" and "salt"
    $people = Array
              (
                    Array('name' => 'Kalle', 'salt' => 856412),
                    Array('name' => 'Pierre', 'salt' => 215863
              )
    );
    
    // for($i = 0; $i < the size of $people, which is two; add one to i)
    for($i = 0; $i < sizeof($people); ++$i)
    {
        // Assign a new value for "salt" on this array item.
        $people[$i]['salt'] = rand(000000, 999999);
    }
    
    ?>
    

  8. Cron uses very little resources, your host shouldn't care at all.

     

    Or do you mean a cron job that I trigger from the php upload page ?

     

    That doesn't make much sense.

     

    As for forking a php script, you could try executing another php script in the cli background via exec.

     

    Why not just use pcntl_fork() ?

  9. <?php
    $array = array("blah.html", "blah.jpg", "blah.txt", "blah2.txt");
    
    $file_ext = array_count_file_types($array);
    
    print(".txt appears ". $file_ext["txt"] ." times.");
    
    function array_count_file_types($arr)
    {
        $types = array();
        
        foreach($arr as $item)
        {
            $types[substr(strrchr(trim($item), '.'), 1)]++;
        }
        
        return $types;    
    }
    ?>
    

     

    Output

    .txt appears 2 times.
    

  10. You have to declare your class before you call it so place the full class above it then call it below. Just switch things around but dont try to declare it twice.

     

    Not true.

     

    <?php
    $world = new Hello;
    $world->say();
    
    class Hello
    {
        public function say()
        {
            print "Hello World!";
        }
    }
    ?>
    

     

    Output

    Hello World!
    

  11. Remove the first instance of ShoppingCart.

     

    <?php
    $cart = new ShoppingCart;
    
    $cart->add_items("Apples", 5);
    $cart->add_items("Oranges", 15);
    $cart->add_items("Peaches", 17);
    
    $cart_items = $cart->show_cart();
    
    foreach($cart_items as $key => $value)
    {
        echo "Item name = $key; Item quantity: $value <br>";
    }
    
    ?>
    
    After adding the items and getting an array that can be used to display them, we quickly add two more functions to remove the items from the shopping cart, and modify the quantity.
    
    <?php
    
    class ShoppingCart {
    
        var $items;
    
        function add_items($product_id, $qty)
        {
           $this->items[$product_id]=$qty;
        }
    
        function update_items($product_id, $qty)
        {
           if(array_key_exists($product_id, $this->items))
           {
              if($this->items[$product_id]>$qty)
              {
                 $this->items[$product_id]-=($this->items[$product_id]-$qty);
              }
              if($this->items[product_id]<$qty)
              {
                 $this->items[$product_id]+=abs($this->items[$product_id]-$qty);
              }
              if($qty==0)
              {
                 unset($this->items[product_id]);
              }
           }
        }
       
        function remove_item($product_id)
        {
           if(array_key_exists($product_id, $this->items))
           {
              unset($this->items[$product_id]);
           }
        }
    
        function show_cart()
        {
           return $this->items;
        }
    
    }
    
    $cart = new ShoppingCart;
    
    $cart->add_items("Apples", 5);
    $cart->add_items("Oranges", 15);
    $cart->add_items("Peaches", 17);
    
    $cart_items = $cart->show_cart();
    
    foreach($cart_items as $key => $value)
    {
        echo "Item name = $key; Item quantity: $value <br>";
    }
    
    $cart->update_items("Peaches", 28);
    $cart->update_items("Oranges", 7);
    $cart_items=$cart->show_cart();
    
    echo "================<br>";
    
    foreach($cart_items as $key=>$value)
    {
        echo "$key = $value<br>";
    }
    
    $cart->remove_item("Oranges");
    $cart_items=$cart->show_cart();
    
    echo "================<br>";
    
    foreach($cart_items as $key=>$value)
    {
        echo "$key = $value<br>";
    }
    
    
    ?> 
    

  12. <?php
    $page = "http://www.youtube.com/browse?s=bzb";
    $page_contents = file_get_contents($page);
    
    $names = preg_match_all('/<span class="video-username"><a id="video-from-username-.*" class="hLink"  href="\/user\/(.*)">.*<\/a><\/span>/', $page_contents, $matches);
    print_r($matches[1]);
    
    ?>
    

    Thank you so much, this is exactly what I was looking for!

     

    How would I list them without the array information? For example, make it say this:

    user1, user2, user3

     

     

     

     

    This is more flexible:

     

    foreach ($matches[1] as $value) {
    echo   $value; // add anything you want in front of or after 
    }
    

     

    Sure if you want your result to be.

     

    , name, name, name, name, 

     

     

  13. <?php
    $page = "http://www.youtube.com/browse?s=bzb";
    $page_contents = file_get_contents($page);
    
    $names = preg_match_all('/<span class="video-username"><a id="video-from-username-.*" class="hLink"  href="\/user\/(.*)">.*<\/a><\/span>/', $page_contents, $matches);
    print_r($matches[1]);
    
    ?>
    

     

    Output

    Array
    (
        [0] => ifbnews
        [1] => sigves
        [2] => lunamaria0708
        [3] => PrismWeapon
        [4] => sugoidrama070109
        [5] => ebonite45
        [6] => AluminumFoils
        [7] => VenetianPrincess
        [8] => NGRNinfa
        [9] => cheezburger
        [10] => MootPoon817
        [11] => SouljaBoy
        [12] => NationalGeographic
        [13] => jobros1love1dream2
        [14] => cplfreeman
        [15] => 10LMessi7CRonaldo
        [16] => betamaxdc
        [17] => failblog
        [18] => DjGhostM
        [19] => jameslikecoulter
        [20] => XxNewXDisneyxX
        [21] => MondoMedia
        [22] => NokiaConversations
    )
    

  14. I'm assuming you've set up your users table with an ID column, if so

     

    mysql_query("UPDATE members SET counter = counter + 1 WHERE user_id = '12345'");
    

     

    Where user_id is the column name containing the ID, and 12345 is the ID.

     

    If you don't have an ID column,

     

    mysql_query("UPDATE members SET counter = counter + 1 WHERE username = 'bob'");
    

     

    Where username is the column name containing the username, and bob is the name.

     

    See MySQL: 12.2.11. UPDATE Syntax for more information.

     

  15. <?php
    function showprogress()
    {    
        $progressfile = "text.txt";
        $content = null;
        
        $arr = file($progressfile);
        $count = count($arr);
        
        list($percentcomplete, $status) = explode("||", $arr[$count], 2);
        
        $content .= '<div class="progressbar" style="width:'.$percentcomplete.'%">'.$percentcomplete.'%</div>';
        $content .= '<div class="statusupdate">'.$status.'</div>';
        
        echo $content;
    }  
    ?>
    

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.