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 4581154
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:53:06+00:00 2026-05-21T20:53:06+00:00

I’m displaying a story on a website, I would like to dynamically add an

  • 0

I’m displaying a story on a website, I would like to dynamically add an image within the story, how could I do that?

I don’t want to add it at the beginning of the story or at the end of the story, but dynamically embed it somewhere in the middle of the story with either a float left or right.

I’m using PHP and jQuery and the story could have HTML in it as well. If I have a story like so, how can I embed the image?

Nullam nibh lorem, aliquam sed sodales
sed, egestas at tortor. Curabitur
commodo urna non elit scelerisque ac
rutrum tellus interdum. Phasellus arcu
leo, congue eu ultrices vitae,
pulvinar vitae neque. Sed tempor dolor
eu eros aliquet in egestas metus
bibendum. Etiam eleifend tellus vitae
purus consectetur eget venenatis
ligula cursus. In hac habitasse platea
dictumst. Nulla in elit erat.
Vestibulum mattis malesuada sapien
quis sodales. Sed id tortor id odio
mollis iaculis eu a sem. Morbi
pellentesque lorem vitae ante molestie
<img src="pathtoimage" style="float:left;"> sit amet pulvinar
lorem porta. Mauris tempor laoreet
quam vel tristique. Donec ultrices
porttitor sapien, eu consectetur
tortor rhoncus eget. Nulla vehicula
magna nisi. Aenean consectetur
placerat laoreet. Praesent blandit
quam turpis, non egestas elit. Quisque
dolor turpis, sollicitudin id
elementum sit amet, sodales at metus.
Duis tortor neque, scelerisque a
adipiscing quis, blandit non velit.
Donec mattis, nibh at pharetra porta,
erat massa tempor quam, at hendrerit
sem nibh sit amet magna. Sed auctor
lectus ac magna accumsan commodo. Nam
vehicula velit et neque eleifend sit
amet eleifend leo euismod.

  • 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-05-21T20:53:07+00:00Added an answer on May 21, 2026 at 8:53 pm

    Since the story can contain, HTML, the problem can get very messy. The following algorithm will insert an image an arbitrary percentage into the story, and make sure it’s not inside an HTML tag. Note that it sacrifices efficiency for straightforwardness.

    Warning: This code can still insert the image tag somewhere it doesn’t belong, depending on what HTML the story allows. For example, if inline script tags are allowed1, the algorithm could have “fun” effects. 😉

    See the live demo at:   jsbin.com/agimi5/3.

    Suppose, the story is all inside <div id="StoryGoesHere">. Then insert the image with, say:

    InsertImageInStory ("StoryGoesHere", 30, "F_left", "Image URL");
    

    Given:

    function InsertImageInStory (ContainID, PercentIn, LeftorRightClass, ImageURL)
    {
        var storyContainer  = $("#" + ContainID);
        var storyHTML       = storyContainer.html ();
        var storyLen        = storyHTML.length;
        var targetCharPos   = (storyLen * PercentIn) / 100;
    
        /*--- Now Loop through the story until we reach the target character, WHILE
            making sure it isn't inside an HTML tag.
        */
        for (var J = 0, bInTag = false;  J < storyLen;  ++J)
        {
            if (storyHTML.charAt(J) == '<'  ||  storyHTML.charAt(J) == '>' )
                bInTag              ^= true;    //-- Toggle value.
    
            if (J < targetCharPos)  continue;
    
            /*--- We've found/passed the target position.  Insert the image just as soon
                as we are clear of any tags.
            */
            if (!bInTag)
            {
                var newStory    = storyHTML.substr (0, J+1);
                newStory       += '<img src="' + ImageURL + '" class="' + LeftorRightClass + '">';
                newStory       += storyHTML.substr (J+1, storyLen+1);
    
                storyContainer.html (newStory);
                break;
            }
        }
    }
    

    The algorithm can be easily converted to PHP, if you prefer to do the manipulation server-side.


    1 If inline script tags are allowed, you’ve got much bigger problems, anyway.

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

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.