Example probably works best.
-
a|b|c
needs to becomearray('a', 'b', 'c')
-
a|\||\}
needs to becomearray('a', '\|', '\}')
-
ab\}aaa|ae\|aa
needs to becomearray('ab\}aaa', 'ae\|aa')
The string that's going to be transformed can have any type of characters, but there are 3 "special" characters that can be interpreted as a straightforward character, only if it is escaped with \
. |
separates an option but, if escaped, needs to be interpreted as an option or a part of it (like any other character). { and }
are always going to be escaped at this point.
The catch is that I need to do this without using regular expressions.
I have been struggling with this one for 10 hours, and I sure hope anyone has a simple answer to this.
***Edit
My plan was to search for a |
and if found, check if it is escaped. If yes, then continue searching for the next one. When I find |
, I would take out the first option of the string, and continue the same way, until there were no |
left.
while ($positionFound != 1) {
$intPrevPosition = $intPosition;
$intPosition = strpos($strTemp, '|', $intPosition);
if ($intPosition === false || (substr_count($strTemp, '|') == 1 && $strTemp{$intPosition + $intPrevPosition - 1} == '\\')) {
$arrOptions[] = $strTemp;
$positionFound = 1;
}
elseif ($strTemp{$intPosition + $intPrevPosition - 1} != '\\') {
$intPosition = $intPrevPosition + $intPosition;
$arrOptions[] = substr(substr($strTemp, 0, $intPosition + 1), 0, -1);
$strTemp = substr($strTemp, $intPosition + 1);
$intPosition = 0;
}
}