Try this; will provide a detailed explanation so the whole concept of the regex is simplified doesn’t seem like magic:
$string = "startTHISISTHESTRINGINEEDend";
preg_match("/^start([a-z0-9]+)end$/is", $string, $matches, null, 0);
echo '<pre>';
print_r($matches);
echo '</pre>';
The regex is fairly simple. And here is the explanation.
- Match
start at the beginning of the string; that is what the ^ means.
- Next the parentheses of
( and ) basically mean you are capturing what are in between the parentheses.
- So in between the
( and ) is regex logic to capture only alphanumeric characters as represented by [a-z0-9]+.
- Match
end at the end of the string; that is what the $ means.
- The
is at the end of the the regex basically means make sure the match is case insensitive via the i (aka: PCRE_CASELESS) and then the s indicates a PCRE_DOTALL which as explained in the PHP manual on pattern modifiers:
If this modifier is set, a dot metacharacter in the pattern matches
all characters, including newlines. Without it, newlines are excluded.
This modifier is equivalent to Perl's /s modifier. A negative class
such as [^a] always matches a newline character, independent of the
setting of this modifier.
If you wish to include non alpha numeric characters you can just use this (.*?) instead of ([a-z0-9]+). But unclear from your request since you are only showing alphabet characters. Or if you wanted to capture specific non-alphanumeric characters like %, / & ^ then just do this: ([a-z0-9%\/^]+). Note how the / is set as \/. Adding the \ escapes the / which makes preg_match realize that it needs to explicitly match / & not interpret that as part of the regex logic.
And the output of $matches would be:
Array
(
[0] => startTHISISTHESTRINGINEEDend
[1] => THISISTHESTRINGINEED
)
So just access it by referring to $matches[1].
echo $matches[1];
Output would be:
THISISTHESTRINGINEED