preg_replace(array('#/(.?)#e', '/(^|_|-)+(.)/e'),
array("'::'.strtoupper('\\1')", "strtoupper('\\2')"), $text);
I've never used regex this way,how does it work?
preg_replace(array('#/(.?)#e', '/(^|_|-)+(.)/e'),
array("'::'.strtoupper('\\1')", "strtoupper('\\2')"), $text);
I've never used regex this way,how does it work?
Does it even work?
echo camelize('http://google.com/');
Result:
Http:::/google.com::
Most of how it "works" can be found on the documentation for preg_replace. It uses the array form to make multiple replacements (see example 2). It uses the e
switch to execute PHP code instead of making a string replacement.
usually its: preg_replace('/pattern/flags', $replacement, $text)
.
here first and second arguments are array of the same size, it's like you call preg_replace for each $element of the arrays.
secondly, /
is usually the pattern delimiter but in fact you can use any character, just use it as the first and its the delimiter. (here #
is used in the first pattern)
in the replacement string, \\1
(which is \1
escaped) means the content of the first parenthesis matched and \2
is the second match.
in this case \1
is what is match by .?
in the first pattern and \2
is what `.\ matched in the second pattern
Replace what is matched by /(.?)
with '::'.strtoupper('\\1')
where \1
is replaced by what is matched in regex group 1: (.?)
And replace what is matched by (^|_|-)+(.)
with strtoupper('\\2')
where \2
is replaced by what is matched in regex group 2: (.)
regex #1: /(.?)
means:
/ # match the character '/'
( # start capture group 1
.? # match any character except line breaks and match it once or none at all
) # end capture group 1
and regex #2: (^|_|-)+(.)
means:
( # start capture group 1
^ # match the beginning of the input
| # OR
_ # match the character '_'
| # OR
- # match the character '-'
)+ # end capture group 1 and repeat it one or more times
( # start capture group 2
. # match any character except line breaks
) # end capture group 2
Note that ^
matches the start of the input, not the literal ^
!