| 1 |
<?php |
| 2 |
|
| 3 |
class VP_Util_Text |
| 4 |
{ |
| 5 |
|
| 6 |
public static function parse_md($text) |
| 7 |
{ |
| 8 |
if(!function_exists('Markdown')) |
| 9 |
{ |
| 10 |
$path = VP_FileSystem::instance()->resolve_path('includes', 'markdown/parser'); |
| 11 |
require $path; |
| 12 |
// require VP_INCLUDE_DIR . '/markdown/parser.php'; |
| 13 |
} |
| 14 |
return Markdown($text); |
| 15 |
} |
| 16 |
|
| 17 |
public static function make_opt($optArray) |
| 18 |
{ |
| 19 |
$optString = ""; |
| 20 |
foreach ($optArray as $key => $value) |
| 21 |
{ |
| 22 |
$optString .= "(" . $key . ":" . $value . ")"; |
| 23 |
} |
| 24 |
return $optString; |
| 25 |
} |
| 26 |
|
| 27 |
public static function print_if_exists($value, $format) |
| 28 |
{ |
| 29 |
if (!empty($value)) |
| 30 |
{ |
| 31 |
if (is_array($value)) |
| 32 |
{ |
| 33 |
$value = implode($value, ', '); |
| 34 |
} |
| 35 |
call_user_func('printf', $format, $value); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
public static function return_if_exists($value, $format) |
| 40 |
{ |
| 41 |
$result = ''; |
| 42 |
if (!empty($value)) |
| 43 |
{ |
| 44 |
if (is_array($value)) |
| 45 |
{ |
| 46 |
$value = implode($value, ', '); |
| 47 |
} |
| 48 |
$result = call_user_func('sprintf', $format, $value); |
| 49 |
} |
| 50 |
return $result; |
| 51 |
} |
| 52 |
|
| 53 |
public static function out($string, $default) |
| 54 |
{ |
| 55 |
if( empty($string) ) |
| 56 |
echo $default; |
| 57 |
else |
| 58 |
echo $string; |
| 59 |
} |
| 60 |
|
| 61 |
public static function prefix(&$item, $key, $prefix) |
| 62 |
{ |
| 63 |
$item = $prefix . $item; |
| 64 |
} |
| 65 |
|
| 66 |
public static function prefix_array($array, $prefix) |
| 67 |
{ |
| 68 |
array_walk( $array, 'VP_Util_Text::prefix', $prefix); |
| 69 |
return $array; |
| 70 |
} |
| 71 |
|
| 72 |
public static function starts_with($haystack, $needle) |
| 73 |
{ |
| 74 |
return !strncmp($haystack, $needle, strlen($needle)); |
| 75 |
} |
| 76 |
|
| 77 |
public static function ends_with($haystack, $needle) |
| 78 |
{ |
| 79 |
$length = strlen($needle); |
| 80 |
if ($length == 0) |
| 81 |
{ |
| 82 |
return true; |
| 83 |
} |
| 84 |
return (substr($haystack, -$length) === $needle); |
| 85 |
} |
| 86 |
|
| 87 |
public static function flanked_by($haystack, $left, $right = '') |
| 88 |
{ |
| 89 |
if( $right == '' ) |
| 90 |
$right = $left; |
| 91 |
return (self::starts_with($haystack, $left) and self::ends_with($haystack, $right)); |
| 92 |
} |
| 93 |
|
| 94 |
} |