0

Have trouble with this and have looked back at previous answers but still seem not to be getting it. Here is the code:

preg_replace('/^.*"([http|https]://test.co/v/.*)/embed.*width=".*$/', '$1', $str);

Am getting a Unknown modifier '/' error.

Looking at the previous answers it looks like I have to deliminate the / that is used in the query so I added a ~ before each forward slash but still the same error. Am guessing I didn't quite understand it... any suggestions appreciated!

OLM256
  • 95
  • 1
  • 7
  • You're delimiting your pattern with `/`, then using `/` within the pattern itself. so `/^...https]:/` is the start and end of your pattern, and everything else after that point is a modifier and outside of the pattern. – Marc B Dec 16 '14 at 16:39
  • You should also change `[http|https]` to `(http|https)` and change `test.co` to `test\.co` or `test[.]co`. – hellcode Dec 16 '14 at 17:04
  • Thanks for your help and replies! Yes the question was similar to previous ones - as I mentioned I had read them but was still have difficulties implementing it on this particular code. All resolved now thankfully. – OLM256 Dec 17 '14 at 05:20

1 Answers1

4

Escape the / in the regex as

preg_replace('/^.*"((?:http|https):\/\/test\.co\/v\/.*)\/embed.*width=".*$/', '$1', $str);

OR

You can use a different delimter eg #

preg_replace('#^.*"((?:http|https:)//test\.co/v/.*)/embed.*width=".*$#', '$1', $str);

Note

  • You can shorten http|https as https?

  • Escape the . in .co as \.co

nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
  • Thanks very much for your help - much appreciated and apologies for the slow reply! The person who wrote it left several other mistakes which was a problem but your help resolved this one. – OLM256 Dec 17 '14 at 05:17