1

I have a string like this:

$string = 'startTHISISTHESTRINGINEEDend'

I need the string between start and the end obviously. I tried regex but its way hard for a newbie like me so no succes on that side. The length of end part is changing so substr function is no go. Tried to combine strpos with strstring but no success on that too. How can I achieve that?

5 Answers 5

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

I'll remove my answer in favor of yours as you have a PHP specific solution. Could you just jot a quick edit down explaining what the [a-z0-9]+ does for this poster's sake?
Holy cow. I think you just answered 50% of all regex questions on Stack Overflow.
That's really a good explanation. Thanks a lot. But I didn't get the [a-z0-9]+ part. So characters like % / ^ will be ignored then?
0
$string = str_replace("start","", $string);
$string = str_replace("end","", $string);
echo $string;

Not a wise solution but probably sufficient one

Comments

0
start(.*?)end

then get Group#1. In demo look at Group#1.

Comments

0

If your start and end occurs somewhere in a string (only once) and you don't want to use regular expressions, you can use the following solution:

$string = 'HIstartTHISISTHESTRINGINEEDendANDOTHERSTUFFHERE';
$start = strpos($string, 'start') + strlen('start');
$end = strrpos($string, 'end');
$result = substr($string, $start, $end-$start);
var_dump($result);

Comments

0
$string = 'xwerwstartTHISISTHESTRINGINEEDasdwerendwerq';

$start = 'start';
$stop = 'end';

echo substr($string, 
    strlen($start) + strpos($string, $start), 
    strpos($string, $stop) - (strpos($string, $start) + strlen($start))
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.