0

I use the the following pattern in preg_replace() to replace a specific domain name from the beginning of all paths in html file. Everything is fine, but the problem is that it replaces the domain names in email addresses also.

preg_replace('%(https?://)?(www\.)?domain\.com/?%im', '', '$html')

Result shold be like:

domain.com/path/ => /path/
www.domain.com/path/ => /path/
11
  • Can you clarify what you mean by beginning of file path in html file; or provide a functional example string? Also the m modifier isn't doing anything in your expression. Commented Oct 30, 2015 at 16:19
  • Something like: %(https?://)?(?<!@)(www\.)?domain\.com/?%im negating the @? Commented Oct 30, 2015 at 16:21
  • 2
    you made the protocol portion optional, so there's no way for the regex to tell between example.com-the-email-address and example.com-the-web-url Commented Oct 30, 2015 at 16:23
  • @AbraCadaver that works somehow. Commented Oct 30, 2015 at 16:26
  • 1
    You don't need the + although https://https://www.domain.com would be strange. The ? made it optional; just removing that requires it be present once. Commented Oct 30, 2015 at 16:30

1 Answer 1

2

If you want to replace:

But not email address with that domain then you need to use a negative lookbehind for the @ that is required in an email address:

'%(https?://)?(?<!@)(www\.)?domain\.com/?%im'

You don't need the m modifier and if you don't want to replace the trailing / then remove it:

'%(https?://)?(?<!@)(www\.)?domain\.com%i'
Sign up to request clarification or add additional context in comments.

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.