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