-1

I am trying to replace the content between two words using php. The content between the two words is different so I can't use tradition str_replace. I want to replace the content between two words for example:

I would like to replace **some string of text** between two words

change to:

I would like to replace between two words

You can see that I removed all the wording between "some" and "text". Again I cannot use regular str_replace because the text between the two words may differ. For example it may say:

I would like to replace **some words of text** between two words

change to:

I would like to replace between two words
j08691
  • 204,283
  • 31
  • 260
  • 272
user982853
  • 2,470
  • 14
  • 55
  • 82
  • `$txt = preg_quote('some string of text'); $new = preg_replace("/$txt/", 'new text', $old);` doesn't work? – Marc B Sep 30 '12 at 19:32

2 Answers2

3

The regex is simple: /some .*? text/

Just replace it with the empty string.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

According to your question, only the inner part of your string changes. If that is the case it's rather trivial, because you already have the solution: You do not need to replace it, but you just need to not take it over:

$result = substr($string, 0, $startlen) . substr($string, -$endlen);

Probably this helps you to find some more "resolution angles" for such problems.

hakre
  • 193,403
  • 52
  • 435
  • 836