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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:55:47+00:00 2026-06-16T00:55:47+00:00

I am using simple_html_dom manage the way CMS driven images are embed on a

  • 0

I am using simple_html_dom manage the way CMS driven images are embed on a page. However I would like to be able to wrap any images that aren’t already wrapped in anchor tags in a new anchor tag, but I can’t work out how to wrap a simple_html_dom element with a new tag.

My code so far:

$content = str_get_html($content);
if(is_object($content))
{
    $elements = $content->find('img');
    foreach($elements as $element)
    {
/*
GET INFORMATION ABOUT WHAT IMAGE THIS IS - ALL FINE
*/          
$classStr = $element->class;
        if(trim(strlen($classStr)) > 0)
        {
            $needle = "wp-image-";
            $after = substr($classStr, strpos($classStr, $needle) + strlen($needle));
            if(strpos($after, " "))
            {
                $imageID = substr($after, 0, strpos($after, " "));
            }
            else
            {
                $imageID = $after;
            }   
/*
Get new image to link to
*/
$image = tona_get_image_by_id($imageID, "full");

/*
CHECK IF PARENT OF IMAGE IS AN ANCHOR... IF SO CHANGE THE LINK
*/

            $elementParent = $element->parent();
            if(isset($elementParent->href)) 
            {
                $elementParent->href = $image["src"];
                $elementParent->class .= " newAnchorClass ";    
            }
/*
IF NOT ADD A NEW ANCHOR TAG ** HELP **
*/

            echo "IMG: " . $imageID . " " . $image["src"] . "<br/>";    
            $element->href = $image["src"];
        }
    }
}

Thanks in advance for your help.

  • 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-16T00:55:48+00:00Added an answer on June 16, 2026 at 12:55 am

    From the documentation under “Tips” tab:
    $e->outertext = '<div class="wrap">' . $e->outertext . '<div>'

    If you want to further manipulate the element, you can use temporary variable to recreate the dom. In your case, it would be:

    // '$element' is the element to be wrapped
    $tempHtml = str_get_html('<a>' . $element->outertext . '</a>');
    $link = $tempHtml->find('a', 0);
    // '$src' is the url of the link
    $link->href = $image['src'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using simple_html_dom.php to get all images from an url (like pinterest does) if($_POST['submit'])
I'm scraping a page, however before my content is echoed I would like to
I am using php and simple_html_dom library. I have a page on my site
I'm trying to retrieve images from any site that has images I'm using simplehtmldom
I am using simple_html_dom to parse a website. Is there a way to extract
I'm using simple_html_dom in PHP to get all A-tags inside a html page. But
All, I am accessing a web-page through command prompt using simple_html_dom in php as
for a while I've been using the simple_html_dom include, but I have a question.
I'm using DOM PDF to convert a simple html page to a PDF--everything works
I have a simple HTML table which is bound using knockoutJS. However, I've added

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.