Created
January 17, 2014 20:43
-
-
Save helior/8481076 to your computer and use it in GitHub Desktop.
Remove the first data character and push a string value onto the end of a data representation while preserving its original presentation.
This file contains hidden or 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 | |
/** | |
* Remove the first data character and push a string value onto the | |
* end of a data representation while preserving its original | |
* presentation. | |
* | |
* @param string $char A string value that will be concatenated | |
* to the data series | |
* @param string $string A string representation of data in which | |
* both data and formatting are derived | |
* @return string A string representation | |
*/ | |
function push_char($char, $string) { | |
// Using regular expressions to define "data" for extraction and | |
// abstracting the presentation. | |
$pattern = '/\d|\w/'; | |
// Preserve the template. | |
$template = preg_replace($pattern, '%s', $string); | |
// Capture data into an array. | |
preg_match_all($pattern, $string, $matches); | |
$data = $matches[0]; | |
// Manipulate the data by removing the first item and then | |
// appending $char. | |
array_shift($data); | |
array_push($data, $char); | |
// Apply the new data back to our template. | |
return vsprintf($template, $data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment