preg_match_all matches array - php

I have something like this
$matches = array();
preg_match_all('/(`.+`)(\s+AS\s+`.+`)?/i', '`foo` AS `bar`', $matches);
print_r($matches);
The result is
Array
(
[0] => Array
(
[0] => `foo` AS `bar`
)
[1] => Array
(
[0] => `foo` AS `bar`
)
[2] => Array
(
[0] =>
)
)
So, the question is why I don't have ' AS `bar`' in $matches[2][0]?
(If I remove the '?' symbol from regex, I'll get it, but I need the '?' :))

Quantifiers like + are greedy by default so if the first one can match everything it will do so. Making it non-greedy should do the job:
preg_match_all('/(`.+?`)(\s+AS\s+`.+`)?/i', '`foo` AS `bar`', $matches);
And by the way, $matches = array(); is not necessary - the variable is only written to by preg_match_all so it does not need to be initialized/defined before.
php > preg_match_all('/(`.+?`)(\s+AS\s+`.+`)?/i', '`foo` AS `bar`', $matches);
php > print_r($matches);
Array
(
[0] => Array
(
[0] => `foo` AS `bar`
)
[1] => Array
(
[0] => `foo`
)
[2] => Array
(
[0] => AS `bar`
)
)

Related

How can I convert a string with square brackets into an array?

I got this from the name attribute of a field
name="field[a][2][b][0][c][1][field_name]"
after serializing the form, I got this:
array('field[a][2][b][0][c][1][field_name]'=>'value')
and I need to convert that into the following array:
$field = array (
'a' => array (
[2] => array (
'b' => array (
[0] => array (
'c' => array (
[1] => array (
'field_name'=>'value'
)
)
)
)
)
)
);
do I need some sort of foreach function or php can recognize this string as array?
If you want the result nested, use parse_str().
$text = "field[a][2][b][0][c][1][field_name]=value";
parse_str($text, $result);
print_r($result);
Output:
Array
(
[field] => Array
(
[a] => Array
(
[2] => Array
(
[b] => Array
(
[0] => Array
(
[c] => Array
(
[1] => Array
(
[field_name] => value
)
)
)
)
)
)
)
)
See https://3v4l.org/7nmFT
You can get values in brackets with the regular expression, and then reduce it to the array that you want:
$key = 'field[a][2][b][0][c][1][field_name]';
$value = 'value';
$matches = array();
preg_match_all('/\[([^[]+)\]/', $key, $matches);
$keys = array_reverse($matches[1]);
$result = array_reduce($keys, function ($array, $item) {
return array($item => $array);
}, $value);
Explanation
In the regular expression \[([^[]+)\]:
([^[]+) is matches any symbol except opening bracket, one or more
times, and gets it into the capturing group (I hope you will not have nested brackets);
\[...\] is literally matches brackets around.
The preg_match_all function should populate the $matches array with following data:
Array
(
[0] => Array
(
[0] => [a]
[1] => [2]
[2] => [b]
[3] => [0]
[4] => [c]
[5] => [1]
[6] => [field_name]
)
[1] => Array
(
[0] => a
[1] => 2
[2] => b
[3] => 0
[4] => c
[5] => 1
[6] => field_name
)
)
The $matches[0] have values of a full match and the $matches[1] have values of our first and only capturing group. We have interested only in capturing group values.
Then with the array_reduce function we can simply go through keys in the reverse order, and sequentially wrap our value into an array.
You can use explode function to do it. But the values should have space in between them, for example "Hello World" in which the values would be in Array ( [0] => Hello [1] => world ) but in your case it would be like Array ( [0] => field[a][2][b][0][c][1][field_name] ) until $name as space in-between the characters or word.
$name="field[a][2][b][0][c][1][field_name]"
$name_array = explode(" ",$name);
print_r ($name_array);
When testing your problem on my PHP server with this test code
<form method="post">
<input type="text" name="field[a][2][b][0][c][1][field_name]">
<input type="submit" value="OK">
<form>
<pre>
<?php
if (isset($_POST['field']))
print_r($_POST['field']);
?>
</pre>
I got the following response (after entering the word "hello" into the text box and clicking the "OK" button):
Array ( [a] => Array ( [2] => Array ( [b] =>
Array ( [0] => Array ( [c] => Array ( [1] =>
Array ( [field_name] => hello ) ) ) ) ) ) )
Admittedly, it was formatted nicer, but I am posting from my smartphone, so, please, forgive me for not formatting it again manually.
So, to me it is not quite clear why OP needs extra code to solve his/her problem.
Here is your solution:
https://codebrace.com/editor/b06588218
I have used regex match twice to match variable name and arrays
/[(.+?)]/
matches any values in the array where "?" is lazy matching.
while following regex
/^[^[]+/ matches the variable name.
I have used variable variables to create variable from string extracted from above regex
Result:
Array
(
[a] => Array
(
[2] => Array
(
[b] => Array
(
[0] => Array
(
[c] => Array
(
[1] => Array
(
[field_name] => value
)
)
)
)
)
)
)
I hope this helps

Find all patterns in a string php

This is my string.
$str = '"additional_details":" {"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"}],';
I want to find all patterns that start with "{" and end with "}".
I am trying this:
preg_match_all( '/"(\{.*\})"/', $json, $matches );
print_r($matches);
It gives me an output of:
Array
(
[0] => Array
(
[0] => "{"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"
)
[1] => Array
(
[0] => {"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}
)
)
See the array key 1. It gives all matches in one key and other details too.
I want an array of all matches. Like
Array
(
[0] => Array
(
[0] => "{"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"
)
[1] => Array
(
[0] => {"mode_of_transport":"air"},
[1] => {"mode_of_transport":"air"},
[2] => {"mode_of_transport":"air"}
)
)
What should I change in my pattern.
Thanks
You can use:
preg_match_all( '/({[^}]*})/', $str, $matches );
print_r($matches[1]);
Array
(
[0] => {"mode_of_transport":"air"}
[1] => {"mode_of_transport":"air"}
[2] => {"mode_of_transport":"air"}
)

What's wrong with this regex expression?

i want to preg_match the following code:
{{{/foo:bar/a/0/b}}}
This is my regex (which doesn't work, and i don't understand why):
|{{{\/([[:alpha:]][[:alnum:]\_]*\:[[:alpha:]][[:alnum:]\_]*)(?:\/([[:alnum:]\_]*))+}}}|Uism
Expected result:
Array (
[0] => Array
(
[0] => {{{/foo:bar/a/0/b}}}
)
[1] => Array
(
[0] => foo:bar
)
[2] => Array
(
[0] => a
)
[3] => Array
(
[0] => 0
)
[4] => Array
(
[0] => b
)
)
The result i get:
Array (
[0] => Array
(
[0] => {{{/foo:bar/a/0/b}}}
)
[1] => Array
(
[0] => foo:bar
)
[2] => Array
(
[0] => b
)
)
I only get the last element back. So what's wrong with it?
You're repeating the second capturing group:
(?:
\/
(
[[:alnum:]\_]*
)
)+
On each repetition of the outer non-capturing group, the contents of the inner capturing group are overwritten, which is the reason why only the last match is preserved. This is standard behavior across all regex engines.
(?=(^.*$)|(?:\/(.*?)(?:\/|})))
Try this.See demo.
http://regex101.com/r/lS5tT3/3
Each subsequent match of the same capture group will overwrite the previous one; that's why you end up with just b.
What I would suggest in this case is to match the whole block first and then use a simpler explode() to dig out the inner data; use this expression:
|{{{\/([[:alpha:]][[:alnum:]\_]*\:[[:alpha:]][[:alnum:]\_]*(?:\/[[:alnum:]\_]*)+)}}}|U
Then, with the resulting $matches array (third argument to preg_match()):
$data = explode('/', $matches[1]);
Your pattern is complete overkill for something that should be quite simple:
$rex = "#[{]{3}/(\w+:\w+)/(\w)/(\d)/(\w)[}]{3}#";
$str = "{{{/foo:bar/a/0/b}}}";
preg_match($rex, $str, $res);
Result:
Array
(
[0] => {{{/foo:bar/a/0/b}}}
[1] => foo:bar
[2] => a
[3] => 0
[4] => b
)

preg_match_all and umlets

I am using preg_match_all to filter out strings
The string which I have supplied in preg_match_all is
$text = "Friedric'h Wöhler"
after that I use
preg_match_all('/(\"[^"]+\"|[\\p{L}\\p{N}\\*\\-\\.\\?]+)/', $text, $arr, PREG_PATTERN_ORDER);
and the result i get when I print $arr is
Array
(
[0] => Array
(
[0] => friedric
[1] => h
[2] => w
[3] => ouml
[4] => hler
)
[1] => Array
(
[0] => friedric
[1] => h
[2] => w
[3] => ouml
[4] => hler
)
)
Somehow the ö character is replaced by ouml which I am not really sure how to figure this out
I am expecting following result
Array
(
[0] => Array
(
[0] => Friedric'h
[1] => Wöhler
)
)
Per nhahtdh's comment:
$text = "Friedric'h Wöhler";
preg_match_all('/"[^"]+"|[\p{L}\p{N}*.?\\\'-]+/u', $text, $arr, PREG_PATTERN_ORDER);
echo "<pre>";
print_r($arr);
echo "</pre>";
Gives
Array
(
[0] => Array
(
[0] => Friedric'h
[1] => Wöhler
)
)
If you think preg_match_all() is messy, you could take a look at pattern():
$p = '"[^"]+"|[\p{L}\p{N}*.?\\\'-]+'; // automatic delimiters
$text = "Friedric'h Wöhler";
$result = pattern($p)->match($text)->all();

preg_match_all to get all occurrences of a string

I am trying to find offset of all occurrences with preg_match_all
e.g.
$haystack = 'aaaab';
$needle = 'aa';
preg_match_all('/' . $needle . '/', $haystack, $matches);
$matches is
Array
(
[0] => Array
(
[0] => Array
(
[0] => aa
[1] => 0
)
[1] => Array
(
[0] => aa
[1] => 2
)
)
)
It returns offset of first and second group of aa ("aa" "aa" "b") from the haystack, while I am expecting it to return "aa" starting at index 1 as well.
Array
(
[0] => Array
(
[0] => Array
(
[0] => aa
[1] => 0
)
[1] => Array
(
[0] => aa
[1] => 1
)
[2] => Array
(
[0] => aa
[1] => 2
)
)
)
Is there a way I can fix the regex or use some other function (which accepts regex) to get this done?
PS: I know strpos which can do this, but I have few more things to search for hence will go with preg_match_all.
You'll need to change your needle expression to use an assertion. This will prevent the 2nd a from being eaten by the regular expression engine:
$needle = 'a(?=a)';

Categories