Skip to content

Instantly share code, notes, and snippets.

@cosmomathieu
Last active August 16, 2019 13:28
Show Gist options
  • Save cosmomathieu/6105a1b09fa373cb876786c9fae34b09 to your computer and use it in GitHub Desktop.
Save cosmomathieu/6105a1b09fa373cb876786c9fae34b09 to your computer and use it in GitHub Desktop.
A set of procedual functions to handle common text manipulation tasks in PHP
<?php
/**
* Return the current server date and time to the caller
*
* @param $format Date format
* @return datetime Formated date and time
*/
if( ! function_exists('datetime')){
function datetime($format = 'Y-m-d H:i:s') {
return (new DateTime())->format($format);
}
}
if( ! function_exists('format_phone'))
{
/**
* Example $formatted = format_phone('+11234567890');
*
* @param string $data
* @param string $format
*/
function format_phone($data, $format = 'US') {
switch($format) {
default:
$format = 'US';
}
if( preg_match( '/^\+\d(\d{3})(\d{3})(\d{4})$/', $data, $matches ) )
{
$result = $matches[1] . '-' .$matches[2] . '-' . $matches[3];
return $result;
}
}
}
<?php
if( ! function_exists('rcopy'))
{
/**
* Recursive function to copy entire directories
*
* @see http://php.net/manual/en/function.copy.php#91010
* @param string $src Source directory
* @param string $dst Destination directory
*/
function rcopy($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
rcopy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
}
if( ! function_exists('read_time'))
{
/**
* Calculate readtime
*
* @param string $text
* @param string $message
*/
function read_time($text, $message = 'min read') {
$words = str_word_count(strip_tags($text));
$min = ceil($words / 200);
return $min . ' ' . $message;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment