Reputation: 51
I'm having an issue with preg_match, somehow, I'm still new with preg_match and I still can't get it working like what I want.
Here's my data...
String_Length_Location_Time_Degree_Alt
I'm using this preg_match to split up the data into the varibles...
preg_match('(?<data1>)_(?<data2>)_(?<data3>)_(?<data4>)_(?<data5>)_(?<data6>)', $str, $matches);
And I get the following error "preg_match(): Unknown modifier '_' in [...][...]"
Does anyone have an idea what can be the issue?
Thanks!
Upvotes: 0
Views: 1377
Reputation: 33197
You don't have any delimiters. Delimiters enclose the pattern:
When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.
Typically a "modifier" is a character that sets options for the regular expression (case sensitivity, multiline mode, etc.) and comes after the closing delimiter.
So, this error message is saying that it thinks you are using _
as a modifier, because it seems to be after the pattern. Try enclosing your pattern in the standard delimiter, /
, as in /PATTERN_GOES_HERE/
You also need to match something in the capture groups. .*
will do (match any amount of anything):
preg_match('/(?<data1>.*)_(?<data2>.*)_(?<data3>.*)_(?<data4>.*)_(?<data5>.*)_(?<data6>.*)/', $str, $matches);
print_r($matches);
Output:
Array
(
[0] => String_Length_Location_Time_Degree_Alt
[data1] => String
[1] => String
[data2] => Length
[2] => Length
[data3] => Location
[3] => Location
[data4] => Time
[4] => Time
[data5] => Degree
[5] => Degree
[data6] => Alt
[6] => Alt
)
Alternately, your case looks like a good candidate for explode
, which separates a string into an array splitting the string every time it comes across a delimiter character, which you would specify as the undescore, "_"
.
Upvotes: 3
Reputation: 6148
There are two problems with your code...
Not much to say aparat from the added delimiters /
at each end and the added .*
to capture any character between _
preg_match('/(?<data1>.*)_(?<data2>.*)_(?<data3>.*)_(?<data4>.*)_(?<data5>.*)_(?<data6>.*)/', $string, $match);
You could easily use the explode
function for this as your data is merely separated by a character.
If you want to use regex you could also use preg_match_all
which would make your code much easier to read (assuming that you read regex ;) )
$string = "String_Length_Location_Time_Degree_Alt";
$explode = explode('_', $string);
var_dump($explode);
preg_match_all('/_*(.+?)(?:_|$)/',$string, $matches);
var_dump($matches);
/**
Output for explode:
array(6) {
[0]=>
string(6) "String"
[1]=>
string(6) "Length"
[2]=>
string(8) "Location"
[3]=>
string(4) "Time"
[4]=>
string(6) "Degree"
[5]=>
string(3) "Alt"
}
Output for preg_match_all:
array(2) {
[0]=>
array(6) {
[0]=>
string(7) "String_"
[1]=>
string(7) "Length_"
[2]=>
string(9) "Location_"
[3]=>
string(5) "Time_"
[4]=>
string(7) "Degree_"
[5]=>
string(3) "Alt"
}
[1]=>
array(6) {
[0]=>
string(6) "String"
[1]=>
string(6) "Length"
[2]=>
string(8) "Location"
[3]=>
string(4) "Time"
[4]=>
string(6) "Degree"
[5]=>
string(3) "Alt"
}
}
*/
Upvotes: 0