<html>
<head>
<title>Get URL</title>
<style type="text/css">
<!--
#Layer1 {
    position:relative;
    left:30px;
    top:10px;
    width:820px;
    height:230px;
}
.style4 {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 16px;
    font-weight: bold;
}

-->
</style>
</head>

<body bgcolor="lightyellow">
<?php

    error_reporting(0);                                     //turn off PHP error reporting

//  include "get.php";                                      //include get functions
    include "/home/sultans/web/php/demo/7adv/get.php";      //to make it work with PHP*Tester

    if($_GET)
        retrieve();

    display();

//------------------------------------------------------------------------------------------------------
function retrieve()
{
    global $url, $search, $offset, $length, $msg, $content;

    $url     = $_GET['url'];                                #retrieve user entered data
    $search  = $_GET['search'];
    $offset  = $_GET['offset'];
    $length  = $_GET['length'];

    if ($url == '') {
        $msg = '*** Please enter URL ***';
        return;
    }

    $page = getURL($url);                                   #fetch the URL
    if (! $page) {
        $msg = '*** URL Not Found ***';
        return;
    }
        
    if ($search)
    {   
        $find = strpos($page, $search);                         #find the search token in the page
//      $find = preg_match($search, $page);                     #find the search token in the page using regex

        $content = substr($page, $find+$offset, $length);       #take that substring
        $content = "<h3> $content </h3>";                       #make it large/bold
    }
    else    
        $content = str_replace('<','<',$page);               #display content in text format
}

//------------------------------------------------------------------------------------------------------
function display()
{   
    global $url, $search, $offset, $length, $msg, $content;
?>

    <div id="Layer1">
    <form method="get" action="getURLsnip.php" >

    <fieldset>
    <legend title="Examples:
        https://www.cnbc.com/quotes/AAPL       search=-->USD       252/7
        https://workshop.sps.nyu.edu/~sultans  search=pageName     13/170 
        "> 
    Get a URL Snippet <b>[?]</b></legend>
    <br />
    <table>
    <tr><td><b>URL        <td> <input type="text" name="url"    size="40" value="<?php echo $url    ? $url    : 'https://www.cnbc.com/quotes/AAPL'; ?>" > Example: https://www.cnbc.com/quotes/GOOG
    <tr><td><b>Search for <td> <input type="text" name="search" size="40" value="<?php echo $search ? $search : 'last_timedate' ?>" >                     Example: last_timedate
    <tr><td><b>Offset     <td> <input type="text" name="offset" size="40" value="<?php echo $offset ? $offset : -9;     ?>" >                             Example: -9
    <tr><td><b>Length     <td> <input type="text" name="length" size="40" value="<?php echo $length ? $length :  6;     ?>" >                             Example: 6
    <tr><td><td><input type="submit" value="     Snip It    " />
    <font color='red'> <?php echo $msg; ?> </font></td>
    </table>
    <br />
    </fieldset>
    </form>
    </div>

<?php 
    print "<p id=output> $content </p>";
}
?>

<?php include "../include.php"; ?>      <!-- hyperlink to see the code -->
</body>
</html>