stack
stack

Reputation: 10218

How can I match specific character between specific range in the string?

I have this string:

$str = "here is start of rage, also here is some text and here is the end of string";
//              ^^^^^                                                 ^^^
//                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Now I'm trying to remove all e letter between this range: [start - end]. Well I want this output:

$newstr = "here is start of rag, also hr is som txt and hr is th end of string";

How can I do that?

Upvotes: 1

Views: 173

Answers (4)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

With preg_replace and the \G anchor:

echo preg_replace('~(?:\G(?!\A)|\bstart\b)[^e]*\K(?:\Be|e(?!nd\b))~S', '', $str);

details:

~
(?:
    \G(?!\A)   # contiguous to the previous match, not at the start of the string
  |            # OR
    \bstart\b  # the word "start"
)
[^e]*          # all that is not an "e"
\K             # exclude all previous matched characters from the whole match 
(?:
    \Be        # an "e" that is not the start of a word
  |            # OR
    e(?!nd\b)  # an "e" that is not followed by "nd"
)
~  
S  # the STUDY modifier that tries to improve non-anchored patterns

This pattern find one "e" at a time. Once the word "start" is found, the \G anchor forces the next match to be contiguous, since it matches the position at the end of the previous match. When the word "end" is reached (?:\Be|e(?!nd\b)) fails and the contiguity is broken (until an other eventual word "start").

Note that this pattern doesn't check if the word "end" exists in the string (but it can easily be done). If the word "end" doesn't exist, all "e" will be removed from the word "start" until the end of the string.

Upvotes: 1

Ivan Yonkov
Ivan Yonkov

Reputation: 7034

Not sure why you really want regex for this, but here is one solution

$str = "here is start of rage, also here is some text and here is the end of string";
preg_match_all("/(.*?)start(.*?)end(.*?)$/", $str, $matches);
$newstr = $matches[1][0] . "start" . str_replace("e", "", $matches[2][0]) . "end" . $matches[3][0];
var_dump($newstr);

It captures all before start, all between start and end, and everything after end too. In other words - 3 groups.

The part between start and end should be e-trimmed. The other parts should stay there, so we just restore them.

I believe same can be achieved using preg_replace_callback

Upvotes: 1

alexander.polomodov
alexander.polomodov

Reputation: 5524

Substr and str_replace is your choice for this case

<?php

$str = "here is start of rage, also here is some text and here is the end of string";
//              ^^^^^                                                 ^^^
//                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
$start = 8;
$end = 65;

$newStr =
    substr($str, 0, $start).
    str_replace('e', '', substr($str, $start, $finish - $start)) .
    substr($str, $finish);

var_dump($newStr);

Upvotes: 1

ShaneOH
ShaneOH

Reputation: 1557

You could combine the substring and trim functions.

Use substring to store the three parts of your string: 0 to start, start to end, end to the real end of your string. Then you can use trim on the middle portion, and then output the concatenation of those three parts.

Upvotes: 2

Related Questions