Skip to content

Instantly share code, notes, and snippets.

@helior
Created January 17, 2014 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helior/8481076 to your computer and use it in GitHub Desktop.
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.
<?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