Last active
August 29, 2015 14:15
-
-
Save zeckdude/ebdc913c640fcd19babb to your computer and use it in GitHub Desktop.
Laravel String Helper Functions
From: http://vegibit.com/custom-helper-functions-in-laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This says that when we visit http://you.rock/stringhelpers, we’ll initialize a basic string which will contain the text We like to program in PHP and we like to use the Laravel Framework! Then we’ll call our helper functions via static methods and var_dump the result to the screen. | |
string 'We like to program in PHP and we like to use the Laravel Framework!' (length=67) | |
string 'like to program in PHP and we like to use the Laravel Framework!' (length=64) | |
string 'We like to ' (length=11) | |
string 'like to program' (length=15) | |
string ' to ' (length=4) | |
array (size=2) | |
0 => string 'We like to' (length=10) | |
1 => string 'we like to' (length=10) | |
string ' program in PHP and use the Laravel Framework!' (length=47) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Route::get('/stringhelpers', function() { | |
$string = 'We like to program in PHP and we like to use the Laravel Framework!'; | |
var_dump($string); | |
var_dump(Stringhelpers::split_string($string, 'like', 'after', 'include')); | |
var_dump(Stringhelpers::split_string($string, 'program', 'before', 'exclude')); | |
var_dump(Stringhelpers::find_between($string, 'like', 'program', 'include')); | |
var_dump(Stringhelpers::find_between($string, 'like', 'program', 'exclude')); | |
var_dump(Stringhelpers::find_all($string, 'we', 'to')); | |
var_dump(Stringhelpers::delete($string, 'we', 'to')); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Stringhelpers { | |
// string helper functions | |
// Splits a string on a given setpoint, then returns what is before | |
// or after the setpoint. You can include or exclude the setpoint. | |
public static function split_string($string, $setpoint, $beforaft, $incorexc) { | |
$lowercasestring = strtolower ( $string ); | |
$marker = strtolower ( $setpoint ); | |
if ($beforaft == 'before') { // Return text before the setpoint | |
if ($incorexc == 'exclude') { | |
// Return text without the setpoint | |
$split_here = strpos ( $lowercasestring, $marker ); | |
} else { | |
// Return text and include the setpoint | |
$split_here = strpos ( $lowercasestring, $marker ) + strlen ( $marker ); | |
} | |
$result_string = substr ( $string, 0, $split_here ); | |
} else { // Return text after the setpoint | |
if ($incorexc == 'exclude') { | |
// Return text without the setpoint | |
$split_here = strpos ( $lowercasestring, $marker ) + strlen ( $marker ); | |
} else { | |
// Return text and include the setpoint | |
$split_here = strpos ( $lowercasestring, $marker ); | |
} | |
$result_string = substr ( $string, $split_here, strlen ( $string ) ); | |
} | |
return $result_string; | |
} | |
// Finds a string between a given start and end point. You can include | |
// or exclude the start and end point | |
public static function find_between($string, $start, $end, $incorexc) { | |
$temp = self::split_string ( $string, $start, 'after', $incorexc ); | |
return self::split_string ( $temp, $end, 'before', $incorexc ); | |
} | |
// Uses a regular expression to find everything between a start | |
// and end point. | |
public static function find_all($string, $start, $end) { | |
preg_match_all ( "($start(.*)$end)siU", $string, $matching_data ); | |
return $matching_data [0]; | |
} | |
// Uses str_replace to remove any unwanted substrings in a string | |
// Includes the start and end | |
public static function delete($string, $start, $end) { | |
// Get array of things that should be deleted from the input string | |
$delete_array = self::find_all ( $string, $start, $end ); | |
// delete each occurrence of each array element from string; | |
for($i = 0; $i < count ( $delete_array ); $i ++) | |
$string = str_replace ( $delete_array, "", $string ); | |
return $string; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment