Tutorial Force Leading Zero

by in , 0

<?php
  function forceLeadingZero($int) {
    return (int)sprintf('%02d',$int);
  }
?>

Forces leading zero to integers.

was | now

1 | 01
2 | 02
3 | 03

10 | 10
100 | 100
99 | 99

Reference URL

Tutorial Find URLs in Text, Make Links

by in , 0

<?php

// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

// The Text you want to filter for urls
$text = "The text you want to filter goes here. http://google.com";

// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {

       // make the urls hyper links
       echo preg_replace($reg_exUrl, "<a href="{$url[0]}">{$url[0]}</a> ", $text);

} else {

       // if no urls in the text just return the text
       echo $text;

}
?>

The basic function of this is to find any URLs in the block of text and turn them into hyperlinks. It will only find URLs if they are properly formatted, meaning they have a http, https, ftp or ftps.

Check out the comments below for more solutions.

PHP Define Full Path to a File

by in , 0

<?php
  echo dirname(__FILE__);
?>
Save the file as fullpath.php Upload to the folder you'd like to know the full path to Go to http://www.yoursite/lots/of/folders/fullpath.php

You'd might be surprised what shows up. Sometimes you need more than just /lots/of/folders/ as your full path to a file (e.g. .htpasswd). This script will allow you to see that.

Tutorial Find Highest Numerically Named File

by in , 0

$latest = getNewest("/path/to/folder/*_bla.xml");

function getNewest($xmlfile){
   foreach (glob($xmlfile) as $filename) {
       $c = explode('_', basename($filename));
       $files[$c[0]] = $filename;
   }
   ksort($files, SORT_NUMERIC);
   $latest = array_pop($files);
   if (file_exists($latest)){
       return $latest;
   }
   return false;
}

In a folder are files named:
1_bla.xml
2_bla.xml
...
34_bla.xml

The function returns the file with the biggest numeric number:
$latest = "/path/to/folder/34_bla.xml";

Tutorial Find File Extension

by in , 0

Method 1

function findexts ($filename) {

       $filename = strtolower($filename) ;

       $exts = split("[/\\.]", $filename) ;

       $n = count($exts)-1;

       $exts = $exts[$n];

       return $exts;

}

Method 2

$filename = 'mypic.gif';
$ext = pathinfo($filename, PATHINFO_EXTENSION);

Method 3

function getFileextension($file) {
       return end(explode(".", $file));
}

Preg Match All Links on a Page PHP

by in , 0

Here's the basic principal behind spiders.

$html = file_get_contents('http://www.example.com');

$dom = new DOMDocument();
@$dom->loadHTML($html);

// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $hrefs->length; $i++) {
       $href = $hrefs->item($i);
       $url = $href->getAttribute('href');
       echo $url.'<br />';
}

Tutorial Extract Email Addresses

by in , 0

Just pass the string (e.g. the body part of an email) to the function, and it returns an array of Email Addresses contained in the String.

function extract_emails_from($string) {
         preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
         return $matches[0];
}

If you catch the return value of function in $emails, you can parse it using foreach:

foreach($emails as $email) {
    echo trim($email).'<br/>';
}