Let's say, I have this script below in mikrotik router, I want to extract an array with key and value, how to do that if there is another value contains a = character,
add name=100YER on-login=":do {:put \"a\";} on-error={};" rate-limit=256k/512k
The result should be like this:
$result=array ('name'=>'100YER', on-login=>':do {:put \"a\";} on-error={};', 'rate-limit'=>'256k/512k');
I used this regex to split it by =, but the problem is in on-login value.
if (preg_match_all('/[^=]+/i', $response, $MATCHES) ){
//
}
You might use 2 capturing groups with a branch reset group:
{-code-1}
Explanation
{-code-2}
Capture group 1
\w+{-code-2}?:-\w+)*
Match 1+ word chars optionally followed by a-
and 1+ word chars)
Close group 1=
Match literally{-code-2}?
Branch reset group
"{-code-2}
Match"
and start group 2
{-code-2}?:[^"]+{-code-2}?<=\\)")++
Match any char except"
or\"
)"
Close group 2 and match"
Or{-code-2}[^"\s]+)
Capture group 3, match any char except"
or a whitespace char)
Close branch reset groupFor example
$re = '/{-code-2}\w+{-code-2}?:-\w+)*)={-code-2}?"{-code-2}{-code-2}?:[^"]+{-code-2}?<=\\\\)")++)"{-code-2}[^"\s]+))/';
$str = 'add name=100YER on-login=":do {:put \\"a\\";} on-error={};" rate-limit=256k/512k';
preg_match_all{-code-2}$re, $str, $matches);
$result = array_combine{-code-2}$matches[1], $matches[2]);
print_r{-code-2}$result);
Output
Array
{-code-2}
[name] => 100YER
[on-login] => :do {:put \"a\";} on-error={};
[rate-limit] => 256k/512k
)
To improve upon @Thefourthbird's pattern, use the wisdom from the "best" technique at PHP: Regex to ignore escaped quotes within quotes. Not only does this improve pattern efficiency in terms of step count, it also more accurately differentiates backslashes that are used literally versus used for escaping.
Otherwise, I completely concur that a branch reset is most suitable for keeping the targeted substrings in consistent columns ofpreg_match_all()
's output array.
Code: (Demo)
$string = <<<MIKROTIK
add name=100YER on-login=":do {:put \"a\";} on-error={};" rate-limit=256k/512k
MIKROTIK;
var_export(
preg_match_all(
'~(\w+(?:-\w+)*)=(?|"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|([^" ]+))~',
$string,
$out
)
? array_combine($out[1], $out[2])
: []
);
Output:
array (
'name' => '100YER',
'on-login' => ':do {:put \\"a\\";} on-error={};',
'rate-limit' => '256k/512k',
)
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.