john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

php functions

php-functions

http://uk3.php.net/function.include

vars.php
<?php

$color = 'green';
$fruit = 'apple';

?>

test.php
<?php

echo "A $color $fruit"; // A

include 'vars.php';

echo "A $color $fruit"; // A green apple

?>


// works
if ((include 'vars.php') == 'OK') {
    echo 'OK';
}

readfile()      //only outputs from text file, can't "run" php on server

NOTE: include 'filename.php' is not much different from file_get_contents()

---------------------------------------------------------------------------

function file_exists_and_writable($filename, $file_open_mode)
{
    if (! $fp = fopen($filename, $file_open_mode) )
    {    echo "Cannot open file";
         exit;
    }

    if(! is_writable($filename) )
    {
        clearstatcache();
        echo "Cannot write to ".$filename." - Check permissions.";
        exit;
    }

    return $fp;   // filename can be opened and written to
}

function get_ip_address()   // get accurate ip address of client
{
   if (getenv(HTTP_X_FORWARDED_FOR))
    {      $ip_temp = getenv(HTTP_X_FORWARDED_FOR);
    }
    else{   $ip_temp = getenv(REMOTE_ADDR); }

    return $ip_temp;
}



function send_query_get_contents_string($query)
{
    $query = "http://google.com/search?q=" . $query . "&num=100";       //get top 100 results

    echo '<a href="' . $query . '">' . $query . '</a>';
    $result = file_get_contents($query);         //downloading the resulting page from google

    if( $result == false )
    {   exit("error getting contents from google"); }

    return $result;
} //end func send_query_get_contents_string

function process_string_for_ranking($result, $target_url)
{
    //echo "<br />\nDEBUGGING: ". $result . "<br />\n";
    $ranking_array;

    $result_array = explode("<h3 class=r>", $result);   //break string into results

    $subarray = explode("</cite>", $result_array[100]);  //cut off data past result 100
    $result_array[100] = $subarray[0];                   //replace with ONLY result 10 data

    unset($result_array[0]);                        //remove google adwords results (arr[0])
    $result_array = array_values($result_array);    //re-index and shift items down

    for( $i=0; $i< sizeof($result_array); $i++)
    {
        $pos = strpos( $result_array[$i], $target_url);

        if( $pos === false )                //strpos requires funny comparison
        {}
        else
        {   echo " " . ($i+1);              //DEBUGGING echo $result_array[$i];
            $ranking_array[] = $i+1;
        }
    }

    return $ranking_array;
} //end func process_string_for_ranking

  • « nslookup
  • php function to extract links into array »

Published

Feb 6, 2010

Category

php

~216 words

Tags

  • functions 7
  • php 82