Skip to content

Instantly share code, notes, and snippets.

@calexandrepcjr
Last active May 25, 2017 13:00
Show Gist options
  • Save calexandrepcjr/f24925ade652455a6a73f4ca713dca22 to your computer and use it in GitHub Desktop.
Save calexandrepcjr/f24925ade652455a6a73f4ca713dca22 to your computer and use it in GitHub Desktop.
A helper to play with Strings, in the future maybe I improve into a library class or better stuff
<?php
/* Function to take all ocurrences between two characters in a string */
$string = '
@if(topLevel)
{{
<form method="post">
@if(mediumLevel)
{{
<input>
}}
</form>
<form method="post">
@if(bottomLevel)
{{
<input>
}}
</form>
}}';
function string_between($string, $start, $end){
$total = substr_count($string, $start);
for ($i = 0; $i < $total; $i++){
$string = ' ' . $string;
$ini = strpos($string, $start);
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
$result[$i] = substr($string, $ini, $len);
$string = substr($string, $len);
$ini = strpos($string, $start);
$string = substr($string, $ini);
}
return $result;
}
function string_between_brackets($string, $start, $end){
$total = substr_count($string, $start);
for ($i = 0; $i < $total; $i++){
$string = ' ' . $string;
$ini = strpos($string, $start);
$ini += strlen($start);
if ($i == 0){
$len = strrpos($string, $end, $ini) - $ini;
} else {
$len = strpos($string, $end, $ini) - $ini;
}
$result[$i] = substr($string, $ini, $len);
$string = substr($string, $ini);
}
return $result;
}
$parsed = string_between($string, '@if(', ')');
$parsedContent = string_between_brackets($string, '{{', '}}');
$i = 0;
$parsedString = array_combine($parsed, $parsedContent);
var_dump($parsedString);
/*array (size=3)
'topLevel' => string '
<form method="post">
@if(mediumLevel)
{{
<input>
}}
</form>
<form method="post">
@if(bottomLevel)
{{
<input>
}}
</form>
' (length=165)
'mediumLevel' => string '
<input>
' (length=19)
'bottomLevel' => string '
<input>
' (length=19)*/
//EXAMPLE
//$string = '"users","actions","movements" "user1","111","54" "user2","87","123" "user3","92","23"';
//END EXAMPLE
//Converts a specific string pattern into an array where the first occurrence of array in string will be converted to index
/* RESULTS
array (size=3)
0 =>
array (size=3)
'users' => string 'user1' (length=5)
'actions' => string '111' (length=3)
'movements' => string '54' (length=2)
1 =>
array (size=3)
'users' => string 'user2' (length=5)
'actions' => string '87' (length=2)
'movements' => string '123' (length=3)
2 =>
array (size=3)
'users' => string 'user3' (length=5)
'actions' => string '92' (length=2)
'movements' => string '23' (length=2)
*/
function string_to_array($string) {
$string = str_replace('"', "", $string);
$result = explode(' ', $string);
$result2 = array();
$i = 0;
//Load the arrays
foreach ($result as $item) {
$result2Array[$i] = explode(',', $item);
$i++;
}
$total = count($result2Array) - 1;
for ($i = 1; $i <= $total; $i++) {
$j = 0;
//Bring values to each index
foreach ($result2Array[0] as $index){
$resultF[$i-1][$index] = $result2Array[$i][$j];
$j++;
}
}
return $resultF;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment