Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP String Helpers
<?php
if (!function_exists('str_combinepath')){
/**
* Combines two strings into a path.
* @param string $path1 : The first path to combine.
* @param string $path2 : The second path to combine.
* @param string $separator : (Optional) The directory separator. Default: OS specified.
*/
function str_combinepath($path1, $path2, $separator = DIRECTORY_SEPARATOR){
$path1 = rtrim($path1, $separator);
$path2 = ltrim($path2, $separator);
return $path1 . $separator . $path2;
}
}
<?php
if (!function_exists('str_contains')){
/**
* Checks if a specified substring occurs within this string.
* @param string $haystack : The string to test.
* @param string $needle : The string to seek.
* @param string $offset : (Optional) Starts searching from specified index.
*/
function str_contains($haystack, $needle, $offset = 0){
return (bool) strpos($haystack, $needle, $offset);
}
/**
* Checks if a specified substring occurs within this string. (case insensitive)
* @param string $haystack : The string to test.
* @param string $needle : The string to seek.
* @param string $offset : (Optional) Starts searching from specified index.
*/
function str_icontains($haystack, $needle, $offset = 0){
return (bool) stripos($haystack, $needle, $offset);
}
}
<?php
if (!function_exists('str_endswith')){
/**
* Check if a string ends with specified string
* @param string $haystack : the string to be checked
* @param string $needle : the string to search
*/
function str_endswith($haystack, $needle) {
return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
}
/**
* Check if a string ends with specified string (case insensitive)
* @param string $haystack : the string to be checked
* @param string $needle : the string to search
*/
function str_iendswith($haystack, $needle) {
return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
}
}
<?php
if (!function_exists('str_isregex')){
/**
* Check if a string given is a valid regex
* @param string $str : the string to be checked
*/
function str_isregex($str){
// https://stackoverflow.com/a/10778344/4469947
return (bool) preg_match("/^\/.+\/[a-z]*$/i", $str);
}
}
<?php
if (!function_exists('str_matchpattern')){
/**
* Check if a string given matches a certain pattern
* @param string $pattern : the reference pattern * -> any, | -> or
* @param string $str : the string to be checked
*/
function str_matchpattern($pattern, $str) {
$pattern = preg_replace_callback('/([^*|])/',
function($m){ return preg_quote("{$m[0]}", "/"); }, $pattern);
$pattern = str_replace('*', '.*', $pattern);
return (bool) preg_match('/^' . $pattern . '$/', $str);
}
/**
* Check if a string given matches a certain pattern (case insensitive)
* @param string $pattern : the reference pattern * -> any, | -> or
* @param string $str : the string to be checked
*/
function str_imatchpattern($pattern, $str) {
$pattern = preg_replace_callback('/([^*|])/',
function($m){ return preg_quote("{$m[0]}", "/"); }, $pattern);
$pattern = str_replace('*', '.*', $pattern);
return (bool) preg_match('/^' . $pattern . '$/i', $str);
}
}
<?php
if (!function_exists('str_startswith')){
/**
* Check if a string starts with specified string
* @param string $haystack : the string to be checked
* @param string $needle : the string to search
*/
function str_startswith($haystack, $needle) {
return (strcasecmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
}
/**
* Check if a string starts with specified string (case insensitive)
* @param string $haystack : the string to be checked
* @param string $needle : the string to search
*/
function str_istartswith($haystack, $needle) {
return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment