Adding Image URL after displaying the Image in Wordpress

I am trying to display the URL of an Image after every image in the main content on Wordpress. To do this I am building a plugin.

If add the following code within my plugin I can display 2 of the same Image. as the code to display the image is stored in $1 below.

$content = preg_replace("/(<img([^>]*)>)/i", "<div class=\"image_wrap\">$1<div class=\"linkbox\">" . "$1</div></div>", $content );

In theory all I need to do is hack at the second instance of $1 in the above code to strip the image src. So below is the code I have tried and it using some substr and strpos functions but that doesn't seem to want to work for me.

$content = preg_replace("/(<img([^>]*)>)/i", "<div class=\"image_wrap\">$1<div class=\"linkbox\">" . substr(substr("$1", strpos("$1","src=\"")+5), 0,strpos(substr("$1",strpos("$1","src=\"")+5),"\"")) . </div></div>", $content );

Is there anything glaringly wrong with this?

Thanks

-------------Problems Reply------------

There are examples for preg_replace_callback in the manual. It usually goes like this:

$content = preg_replace_callback(
"/(<img([^>]*)>)/i",
"cb_image_wrap",
$content
);

function cb_image_wrap($match) {

list($all, $img, $attr) = $match;

return
"<div class=\"image_wrap\">$img<div class=\"linkbox\">" .
substr(substr("$img", strpos("$img","src=\"")+5), 0,
strpos(substr("$img",strpos("$img","src=\"")+5),"\"")) .
"</div></div>";
}

Your string patching code seems needlessly complicated.

It might also appropriate to look into HEREDOC strings.

Here is the solution that I eventually figured out thanks to Mario in the above comments

$content = preg_replace_callback(
'/(<img([^>]*)>)/i',
create_function(
'$matches',
'return CREDIT_HEAD .
"<div class=\"image_wrap\">$matches[0]<div class=\"linkbox\">" .
substr(substr("$matches[0]", strpos("$matches[0]","src=\"")+5), 0,strpos(substr("$matches[0]", strpos("$matches[0]","src=\"")+5),"\"")) .
"</div></div>" .
CREDIT_FOOT;'
),
$content);

Category:php Views:1 Time:2012-03-03

Related post

  • Structuring a URL to display all results for scraping data 2011-07-24

    I am a GIS student working on a project focused on mapping unstructured data, in which I wrote a php script to scrape the following page that displays search results for US GIS job postings, to export to xml, geocode and map. http://gisjobs.com/searc

  • Downloads images from URL and display it in a GridView 2011-06-13

    I have a URL where some images are exists. I want to retrieve all the images from that URL and display these images in the GridView. Now when I click any of thumb preview like in grid view then it should enlarge or load to full screen. putting the sn

  • I'm looking for a Sharepoint Webpart that can query a list for an RSS feed URL, then display it 2011-11-23

    Basically what I have a List that will be maintained by the user that has a field that contains a link to an RSS feed. I tried using the OOTB RSS and it's great, but you have to specify the feed URL and I need that to be based on user selection. For

  • displaying content from a wordpress site in iphone app 2011-05-10

    I am trying to display content from a wordpress site in an iphone app.I downloaded the open source code for the wordpress app and im having difficulty finding the code i need in it.All im trying to do is display the text of a few different pages(no e

  • Validate URL and display as a link in PHP 2011-05-10

    When I display the text from database, I want to detect whether that text is URL and if that's with URL format, i want to hyperlink those text automatically. For example, if my text is like this "Hey, check this out, i found a great website and i wou

  • A href pointed to nowhere added # to url 2011-08-18

    I have a href which pointed to # <a href="#" id="bla" >Bla<a> I have onclick function which displaying popup on click on that a href. function doingClick() { //display popup return false; } But after click symbol # every time added to the

  • #_=_ added to URL by facebook 2011-12-02

    After connecting to my Rails app via Facebook I have #_=_ added on to my URL. I tried specifying a redirect_uri as specified by FB but it didn't work. Javascript workaround to remove the symbols doesn't help. I tried everything with no results. Any p

  • Wordpress automatically adding site url to href, though href not empty 2012-02-05

    So I'm trying to debug a somewhat confusing problem. I'm using a Wordpress plugin for a Topspin store that uses colorbox to popup a window to display more information about an item. Here's the site: okgo.net/store. In the backend code for this page a

  • Why did adding a URL button remove my keyPress ability? 2012-02-06

    A summary of my homework assignment was to create a display and a GUI to interact with that display allowing users to add shapes and move them around along with a few other specifications. I completed that assignment, and added some extra features. O

  • Multiple animated JPanels - only last one added to JFrame is displaying 2009-04-17

    I am making a multithreaded pacman game. There is a PacmanController class which extends JFrame, a Maze class which extends JPanel, a Pacman class which runs in its own thread and extends JPanel, and a Ghost class which runs in its own thread and ext

  • Silverlight User Experience URL Image Displays? 2009-09-08

    I have a database that contains a table for storing URL Images (since storing the images as byte arrays could potentially slow the DB down massively). There are two fields in the image table, one that stores a URL to a low resolution image and one th

  • How do I prevent ASP.NET from converting the & symbol to & when adding a url to a control's attributes? 2010-01-22

    This is what I'm adding and it renders the & as &amp; sharelink.Attributes.Add("addthis:url", "http://" & Request.Url.Host & "/Resources/PublicView.aspx?RID=" & R.ResourceID & "&Key=" & Key) I need it to render this ?R

  • How to pass a parameter to a javascript through a url and display it on a page? 2010-03-08

    For instance, I have a url like this : www.mysite.com/my_app.html How do I pass a value " Use_Id = abc " to it and use javascript to display on that page ? --------------Solutions------------- Shouldn't be too difficult to write your own without the

  • Mod Rewrite MS Hosting Hide Long Annoying URL By displaying the short URL? 2010-03-16

    Scenario : I have a PHP Forum and a Asp.Net site hosted on Linux and MS Hosting respectively (obvious right?!). My domain ABC.com is currently configured with PHP host whereas my ASP.Net site is using the default 1and1 generated URL (s0987465.onlineh

  • django / python get image from url and display on site 2010-03-22

    Given a url to an image is there a way in Django/Python to pull this image in and then display it on my site (resized if possible) Thanks --------------Solutions------------- If you just want to hotlink it print out the html snippet(<img src="http

  • Programmatically added SummaryLinkWebPart doesn't display Links 2010-05-05

    I am using below code to Add SummaryLinkWebPart to a Page and also adding few links to that wehbpart. I can see the webpart now on the page but it doesn't have any links inside it. Does anyone know what is wrong with the code? var wpm = web.GetLimite

  • Adding extra url parameter causes jquery error 2010-10-05

    Intro I'm developing a project with MVC.Net. I have just started a default website with a Home Controller and an Index action. I browse to the view with 'Home/Index/1' and everyting works fine. Now I want to add an extra url parameter, so I've change

  • Joomla TPlancer component adding new field and displaying 2010-10-21

    I have the TPLancer component for Joomla installed on my site and I am trying to add some more information to the profile page of both the Freelancer and the Buyer. I am no genius with PHP or mySQL but can kind of bluff my way thru usually. The infor

  • After URL redirection, url not displaying new address when clicking on links 2011-04-13

    I've got this website, http://www.manndirectory.com, it's a Joomla CMS with the SOBI2 business index installed. I've set it up so that their can be different entries based on their building location. SEF is turned on for both Joomla and SOBI2. I've s

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.105 (s). 11 q(s)