mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Add case insensitive string helper functions. (#4283)
refactor: Refactored new helper functions for case sensitivity #4283
This commit is contained in:
@@ -1376,15 +1376,24 @@ function ResolveGlues($tables, $target, $x = 0, $hist = array(), $last = array()
|
|||||||
/**
|
/**
|
||||||
* Determine if a given string contains a given substring.
|
* Determine if a given string contains a given substring.
|
||||||
*
|
*
|
||||||
* @param string $haystack
|
* @param string $haystack
|
||||||
* @param string|array $needles
|
* @param string|array $needles
|
||||||
|
* @param bool $case_insensitive
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function str_contains($haystack, $needles)
|
function str_contains($haystack, $needles, $case_insensitive = false)
|
||||||
{
|
{
|
||||||
foreach ((array) $needles as $needle) {
|
if ($case_insensitive) {
|
||||||
if ($needle != '' && strpos($haystack, $needle) !== false) {
|
foreach ((array) $needles as $needle) {
|
||||||
return true;
|
if ($needle != '' && stripos($haystack, $needle) !== false) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
foreach ((array) $needles as $needle) {
|
||||||
|
if ($needle != '' && strpos($haystack, $needle) !== false) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -1393,33 +1402,51 @@ function str_contains($haystack, $needles)
|
|||||||
/**
|
/**
|
||||||
* Determine if a given string ends with a given substring.
|
* Determine if a given string ends with a given substring.
|
||||||
*
|
*
|
||||||
* @param string $haystack
|
* @param string $haystack
|
||||||
* @param string|array $needles
|
* @param string|array $needles
|
||||||
|
* @param bool $case_insensitive
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function ends_with($haystack, $needles)
|
function ends_with($haystack, $needles, $case_insensitive = false)
|
||||||
{
|
{
|
||||||
foreach ((array)$needles as $needle) {
|
if ($case_insensitive) {
|
||||||
if ((string)$needle === substr($haystack, -strlen($needle))) {
|
$lower_haystack = strtolower($haystack);
|
||||||
return true;
|
foreach ((array)$needles as $needle) {
|
||||||
|
if (strtolower($needle) === substr($lower_haystack, -strlen($needle))) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
foreach ((array)$needles as $needle) {
|
||||||
|
if ((string)$needle === substr($haystack, -strlen($needle))) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if a given string starts with a given substring.
|
* Determine if a given string starts with a given substring.
|
||||||
*
|
*
|
||||||
* @param string $haystack
|
* @param string $haystack
|
||||||
* @param string|array $needles
|
* @param string|array $needles
|
||||||
|
* @param bool $case_insensitive
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function starts_with($haystack, $needles)
|
function starts_with($haystack, $needles, $case_insensitive = false)
|
||||||
{
|
{
|
||||||
foreach ((array)$needles as $needle) {
|
if ($case_insensitive) {
|
||||||
if ($needle != '' && strpos($haystack, $needle) === 0) {
|
foreach ((array)$needles as $needle) {
|
||||||
return true;
|
if ($needle != '' && stripos($haystack, $needle) === 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
foreach ((array)$needles as $needle) {
|
||||||
|
if ($needle != '' && strpos($haystack, $needle) === 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
80
tests/CommonFunctionsTest.php
Normal file
80
tests/CommonFunctionsTest.php
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* CommonFunctionsTest.php
|
||||||
|
*
|
||||||
|
* -Description-
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* @package LibreNMS
|
||||||
|
* @link http://librenms.org
|
||||||
|
* @copyright 2016 Tony Murray
|
||||||
|
* @author Tony Murray <murraytony@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace LibreNMS\Tests;
|
||||||
|
|
||||||
|
class CommonFunctionsTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testStrContains()
|
||||||
|
{
|
||||||
|
$data = 'This is a test. Just Testing.';
|
||||||
|
|
||||||
|
$this->assertTrue(str_contains($data, 'Just'));
|
||||||
|
$this->assertFalse(str_contains($data, 'just'));
|
||||||
|
|
||||||
|
$this->assertTrue(str_contains($data, 'juSt', true));
|
||||||
|
$this->assertFalse(str_contains($data, 'nope', true));
|
||||||
|
|
||||||
|
$this->assertTrue(str_contains($data, array('not', 'this', 'This')));
|
||||||
|
$this->assertFalse(str_contains($data, array('not', 'this')));
|
||||||
|
|
||||||
|
$this->assertTrue(str_contains($data, array('not', 'thIs'), true));
|
||||||
|
$this->assertFalse(str_contains($data, array('not', 'anything'), true));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testStartsWith()
|
||||||
|
{
|
||||||
|
$data = 'This is a test. Just Testing that.';
|
||||||
|
|
||||||
|
$this->assertTrue(starts_with($data, 'This'));
|
||||||
|
$this->assertFalse(starts_with($data, 'this'));
|
||||||
|
|
||||||
|
$this->assertTrue(starts_with($data, 'thIs', true));
|
||||||
|
$this->assertFalse(starts_with($data, 'test', true));
|
||||||
|
|
||||||
|
$this->assertTrue(starts_with($data, array('this', 'Test', 'This')));
|
||||||
|
$this->assertFalse(starts_with($data, array('this', 'Test')));
|
||||||
|
|
||||||
|
$this->assertTrue(starts_with($data, array('Test', 'no', 'thiS'), true));
|
||||||
|
$this->assertFalse(starts_with($data, array('just', 'Test'), true));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEndsWith()
|
||||||
|
{
|
||||||
|
$data = 'This is a test. Just Testing';
|
||||||
|
|
||||||
|
$this->assertTrue(ends_with($data, 'Testing'));
|
||||||
|
$this->assertFalse(ends_with($data, 'testing'));
|
||||||
|
|
||||||
|
$this->assertTrue(ends_with($data, 'testIng', true));
|
||||||
|
$this->assertFalse(ends_with($data, 'test', true));
|
||||||
|
|
||||||
|
$this->assertTrue(ends_with($data, array('this', 'Testing', 'This')));
|
||||||
|
$this->assertFalse(ends_with($data, array('this', 'Test')));
|
||||||
|
|
||||||
|
$this->assertTrue(ends_with($data, array('this', 'tesTing', 'no'), true));
|
||||||
|
$this->assertFalse(ends_with($data, array('this', 'Test'), true));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,5 +30,6 @@ require_once $install_dir . '/includes/defaults.inc.php';
|
|||||||
|
|
||||||
$classLoader->registerDir($install_dir . '/tests', 'LibreNMS\Tests');
|
$classLoader->registerDir($install_dir . '/tests', 'LibreNMS\Tests');
|
||||||
|
|
||||||
|
require_once $install_dir . '/includes/common.php';
|
||||||
require_once $install_dir . '/includes/rrdtool.inc.php';
|
require_once $install_dir . '/includes/rrdtool.inc.php';
|
||||||
require_once $install_dir . '/includes/syslog.php';
|
require_once $install_dir . '/includes/syslog.php';
|
||||||
|
|||||||
Reference in New Issue
Block a user