Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:13:59+00:00 2026-06-18T11:13:59+00:00

I need to match links, but only those who don’t begin with the string

  • 0

I need to match links, but only those who don’t begin with the string [image:x] where x is the link itself.

I need this in a preg_replace so that only links that fit will be changed, here is an example:

“a dog once ate my book http://mysite.com/dog.jpg and I had to go to
the store https://www.mysite.com/images/store.png and buy a new iPad
apple.com/iPad/iPadMini.jpg And there it was fun
[image:http://apple.com/iPad/images/iPads.com]”

would result as

“a dog once ate my book [image:http://mysite.com/dog.jpg] and I had to go to
the store [image:https://www.mysite.com/images/store.png] and buy a new iPad
[image:apple.com/iPad/iPadMini.jpg] And there it was fun
[image:http://apple.com/iPad/images/iPads.com]”

Notice the last link, as for now I get it doubled (the link is still picked up by preg_replace). like here (not the desired effect, I need it untouched):

...[image:apple.com/iPad/iPadMini.jpg] And there it was fun
[image:[image:http://apple.com/iPad/images/iPads.com]]“

Here’s my preg_replace as for now:

preg_replace('~(https?://)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\S*)\.(jpg|png|jpeg|bmp|gif)~','[[image:$0]]',$text);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:14:01+00:00Added an answer on June 18, 2026 at 11:14 am

    You can use a negative lookbehind.

    We don’t want it to start with ‘[image:’. We also don’t want to start in the middle of a word. this is prevented using \b. If the string starts with http:// it should be included in the match, so don’t match after http:// or https://.

    preg_replace('~(?<!\[image:)(?<!http://)(?<!https://)(?<!\w\.)(https?://)?\b[\w-]+(\.[\w-]+)+\.?(:\d+)?(\S*)\.(jpg|png|jpeg|bmp|gif)~','[image:$0]',$text);
    

    Check this PHP fiddle

    Note: that also won’t replace a string with the final ] missing, like [image:example.com.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need regex to match links like www.example.com in an html/text -string but: not match
I need to add some records to a table, but only those that match
I need to match from a a string \begin{?} and \end{?} where ? is
I need to match a image url like this: http://site.com/site.com/files/images/img (5).jpg Something like this
I need to set up routes wich will match links like this: 1: fixed/9731-monday-tuesday-wednesday-thursday-friday/922-another
I have a collection of links with thumbnails that match them. I need to
I need to match Safari browsers, but not Chrome browsers using one Regex. I
Need help! I've been looking for a solution for this seemingly simple task but
Given a string like this: <a href=http://blah.com/foo/blah>This is the foo link</a> ... and a
I need to match with preg_match the urls of a certain domain that end

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.