Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libraries/Utility] Add startsWith, endsWith string functions to Utility (with example) #2965

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class NDB_Menu_Filter_Help_Editor extends NDB_Menu_Filter
$this->tpl_data['items'][$x][0]['value'] = $x + $count;
$i = 1;
foreach ($item as $key => $val) {
if (substr($key, "Topic") == 0) {
if (Utility::startsWith($key, "Topic")) {
$this->tpl_data['items'][$x][$i]['helpID'] = array_search(
$val,
$help_section
Expand Down
39 changes: 33 additions & 6 deletions php/libraries/Utility.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1226,12 +1226,12 @@ class Utility
}

/**
* Coverts array into CSV string
*
* @param array $array the array to be converted
*
* @return string CSV string of data
*/
* Coverts array into CSV string
*
* @param array $array the array to be converted
*
* @return string CSV string of data
*/
static function arrayToCSV($array)
{
$fp = fopen("php://temp", 'w+');
Expand Down Expand Up @@ -1277,5 +1277,32 @@ class Utility
}
return checkdate($dateElement['M'], $dateElement['d'], $dateElement['Y']);
}

/**
* Determine if a string starts with another string
*
* @param string $haystack string to search
* @param string $needle substring to find
*
* @return boolean if $haystack begins with $needle
*/
static function startsWith($haystack, $needle)
{
return substr($haystack, 0, strlen($needle)) === $needle;
}

/**
* Determine if a string ends with another string
*
* @param string $haystack string to search
* @param string $needle substring to find
*
* @return boolean if $haystack ends with $needle
*/
static function endsWith($haystack, $needle)
{
return substr($haystack, -strlen($needle)) === $needle;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these would make more sense in a new Strings class, rather than adding more stuff to the already overloaded Utility class.

}

?>