szurubooru/src/Helpers/TextHelper.php

38 lines
918 B
PHP
Raw Normal View History

2013-10-05 12:55:03 +02:00
<?php
class TextHelper
{
public static function isValidEmail($email)
{
$emailRegex = '/^[^@]+@[^@]+\.[^@]+$/';
return preg_match($emailRegex, $email);
}
public static function replaceTokens($text, array $tokens)
{
foreach ($tokens as $key => $value)
{
$token = '{' . $key . '}';
$text = str_replace($token, $value, $text);
}
return $text;
}
2013-10-06 13:21:16 +02:00
public static function resolveConstant($constantName, $className = null)
{
//convert from kebab-case to CamelCase
$constantName = preg_split('/-/', $constantName);
$constantName = array_map('trim', $constantName);
$constantName = array_map('ucfirst', $constantName);
$constantName = join('', $constantName);
if ($className !== null)
{
$constantName = $className . '::' . $constantName;
}
if (!defined($constantName))
{
throw new Exception('Undefined constant: ' . $constantName);
}
return constant($constantName);
}
2013-10-05 12:55:03 +02:00
}