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

  • SEARCH
  • Home
  • 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 8961895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:59:52+00:00 2026-06-15T15:59:52+00:00

I want to replace square-braced image placeholders with valid HTML markup. A sample placeholder

  • 0

I want to replace square-braced image placeholders with valid HTML markup.

A sample placeholder might look like this:

[img:http://example.com/_data/025_img.jpg]

I want is to change the bit where it says [img: ... ] with <img> tag and get a result like this:

<img src='http://example.com/_data/025_img.jpg' border='0' />

Additional information about user uploaded images relevant to this task:

  1. user uploads images to their profile
  2. image names are stored in db.
  3. they are listed next to a form which has a textarea
  4. while typing the text, I would like to offer the user to include one OR MORE of their images by adding the following tag [img: ... ] where ... is the link that would be copied upon clicking on the images which are listed from the user gallery.
  5. I’m using Codeigniter and passing the textarea through the view and into the controller->model where it is sanitized by a helper for all sorts of things … sql/quotes etc.. XSS is also enabled on CI;
  6. then I would like to scan the text and see where the user has the [img: ... ] tag and exchange that into a <img> tag and render the post with images followed by text.

So the actual input from the user will be something along the lines of:

The brown fox jumped over foo bar [img:http://example.com/_data/025_img.jpg] and then went to bed [img:http://example.com/_data/0277_img.jpg] while thinking about [img:http://example.com/_data/1115_img.jpg]

That is the reason I asked for preg_replace(), rather than preg_match(). preg_match() doesn’t make the text follow the images.

  • 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-15T15:59:53+00:00Added an answer on June 15, 2026 at 3:59 pm

    Let’s get the easy thing out of the way first.

    /\[img:([^\]]+)\]/
    

    That is:

    • a literal [img:
    • a capture group containing
      • a character class composed of
        • things that are not a literal ]
      • repeating at least once
    • a literal ]

    Run this through preg_match and element 1 in the match array will very likely be an image URL that you can easily insert into an img tag.

    But you shouldn’t. Not right away.

    First, this is insecure as heck. What’s going to happen when I write this?

    [img:javascript:alert(document.cookie);]
    

    Uhoh. That’s not going to be good.

    You’re probably going to want to make sure that the thing that the user claims is a URL really is a URL. You can try doing this by calling parse_url. It will give you back an array of URL components. Make sure that the thing has a domain and a path, and is served over HTTP or HTTPS.

    Okay, but what happens when the user enters this?

    [img:http://www.example.com/foo.jpg" onmouseover="alert(document.cookie)"]
    

    That’s a valid…ish… URL that will be successfully deconstructed by parse_url and may well pass basic checks for well-formedness. Filtering out spaces and quotes (single and double) will be a good starting point, but there are still more things to worry about.

    The bottom line is that markup like this is a vector in XSS, or Cross-site scripting vulnerabilities.

    You can probably mitigate some of the threat by passing the URL through htmlspecialchars. That will at least nuke quotes and brackets, and it’s hard to be nasty with those taken care of. Just watch out for character set sillyness, some non-UTF-8 character encoding can include things that are ASCII quotes…

    You probably want to use a real markup language for this (even if it’s just markdown), and you probably want to use a whitelist-based HTML filter like HTML Purifier on the result. This will help protect you from some levels of insanity.

    Remember, you’re only paranoid if they aren’t out to get you. The web is full of people that are so stupid that they’re malicious, and people that are so malicious that it’s stupid.

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

Sidebar

Related Questions

I want to replace part of the following html text (excerpt of a huge
I want to replace all html codes to empty space. I think I should
I want to replace Demo image by code css3 or somthing else. I have
I want replace default Fancybox1.3.4 image preloader (fancy_loading.png) with another preloader. The Fancybox preloader
I want to replace the contents within a html element so I'm using the
i want to replace html text that closed by tag start_ticker code.... end_ticker i
I want to replace the default title of the header with my image in
I want to replace all pairs of square brackets in a file, e.g., [some
I want to delete all occurrences of square brackets that conform to this regex:
I have a string that looks like this: abc[1,2,3].something.here,foo[10,6,34].somethingelse.here,def[1,2].another I want to split this

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.