I am looking for a regular expression that would also identify and separate commas, the equal sign and any other special characters that I might need in the input.
Right now what I have is $content = preg_split('/[\s]+/', $file_content, -1, PREG_SPLIT_NO_EMPTY);
Which stores the content of the input file into an array where each element is separated by blank spaces.
However for example for function a (int i) {};
the array would look like this:
[0] = function
[1] = a
[2] = (int
[3] = i)
[4] = {};
And what I'd like to achieve with the regular expression is this:
[0] = function
[1] = a
[2] = (
[3] = int
[4] = i
[5] = )
[6] = {
[7] = }
[8] = ;
Use preg_split
function with PREG_SPLIT_DELIM_CAPTURE
flag:
PREG_SPLIT_DELIM_CAPTURE
If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well.
$input = 'function a (int i) {};';
$content = preg_split('/([\p{P}\p{S}])|\s/', $input,
-1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($content);
The output:
Array
(
[0] => function
[1] => a
[2] => (
[3] => int
[4] => i
[5] => )
[6] => {
[7] => }
[8] => ;
)