I am working on a WP website that uses an ecommerce plugin; amongst other things this plugin stores and retrieves product images. Trouble is that, if the product has no image assigned it returns an anchor tag with nothing in the text node as follows:
<a id="product_image_id" href="..."></a>
I'd like to stay away from messing with the core functions of the plugin so that I can stay on the upgrade path and would like to use PHP to check the returned string for an empty text node and replace it with a 'no image available' image like so:
<a id="product_image_id" href="..."><img src="..." /></a>
I've tried all manner of PHP sub string / replace functions without any success and have hit a wall.
Any help would be greatly appreciated!
Glad you solved it!
I had a solution based on SimpleXmlElement in mind:
$xs2 = '<a id="product_image_id" href="..."><img src="..." /></a>';
$xml = new SimpleXMLElement($xs2);
foreach ($xml->children() as $child)
echo $child->getName();
By look at the childern, it is easy to see if there is a image present. I not, add a node and return the SimpleXMLElement->asXml;
For anyone interested, here is how I solved it:
$thumbnail = function_that_returns_formatted_image();
$thumbnail_content = strip_tags($thumbnail, '<img>'); // Strip out </a> and ignore </img>
if (empty($thumbnail_content)) // Check for </img> tag
$thumbnail = '<img src="/path/to/temporary_image.jpg" />';
I hope this helps some one!
Related
I'm locked on something that make me crazy, if you can help me, would be cool !
I have a string containing a valid HTML code. In this PHP code I would like to detect a specific image via the class of his parent, and extract the url of the image + update it with another url.
Here is an example :
$html = '....<div class="header">...<img src="theimage.png" />...</div>...';
I would like to parse the $html string, extract the url "theimage.png" and replace it by "theimage2.png" (after some internal working)
I tried to use a REGEX, but I'm not sure it's the best solution, because I need to be sure that only the image in .header will be returned, and I need to execute some functions to get the name of the future link.
Maybe the solution is to parse the HTML with DOM Node, but it didn't work too.
Can you help me please ? Thanks !!!
You can achieve this using SimplePHPDom
PHP
$html = '<body><div class="header"><p>some html</p><img src="theimage.png" /></div></body>';
$html_dom = str_get_html($html);
foreach($html_dom->find('.header img') as $element) {
echo $element->src . '<br>'; // output src of img inside .header div
}
I have an HTML tag <img> for specific products, however some products don't have images since their similar products already have the same. I want the viewer who reached these image-less products to be able to click on a link and get to the similar products. Now the <img> is like this:
<img src="../imgs/product/<?php echo $productName;?>.png" alt="No image for this item. Please refer to its similar item: <?php echo $alt; ?> "/>
Then I want the $alt part to be a link to the similar product, so I wrote:
<?php
$alt = '<a href=\"singleproduct.php?singleItem='
. $similarProduct .'\">'. $similarProduct. '</a>';
?>
It's not possible to print out the link no matter how I revise the style of quotes. Just wondering if it's not possible at all to insert an <a> inside of <img>...? If not, how to make this work?
You can't have a html tag in a html attibute. If you wan't a full html fallback, take a look at Inputting a default image in case the src attribute of an html <img> is not valid?
I'm currently using a system for my portfolio where i add three images per project, which schould be inside an ul.
This works fine, but i need those img tags to be wrapped within li tags, and i have no clue how i should do this.
Take this:
<img src="img1.jpg"/>
<img src="img2.jpg"/>
<img src="img3.jpg"/>
Make this:
<li><img src="img1.jpg"/></li>
<li><img src="img2.jpg"/></li>
<li><img src="img3.jpg"/></li>
Thanks in advance!
Edit:
Sorry, here's more info for you guys:
I'm using Wordpress 3 as cms with custom post types.
The output is done via wp's "the_content()", where i put my images.
I know i could use wp's html editor to wrap li tags around manually,
the problem is that i wont be maintaining the site, so i want to make it easer for my coworkers.
This is why its outputting the img tags in a row, and i need to wrap those li tags around them.
I've read something about "preg_replace", but i cant seem to get it working.
EDIT:
I've found the solution, sometimes the easiest way is the best:
$thecontent = get_the_content();
$thecontent_format = '<li>'.str_replace(array("\r","\n\n","\n"),array('',"\n","</li>\n<li>"),trim($thecontent,"\n\r")).'</li>';
wraps every img element with a li, and then
echo $thecontent_format;
Anyways, thanks for you participation!
I don't want to sound stupid or anything but if your trying to do this with PHP I am assuming that PHP is also creating the img tags for display.... in that case all you have to do is:
echo "<li>";
image tag generating code goes here
and than close it off with:
echo "</li>";
do this for every image tag that being created...
Is that what you were asking?
I want to use phpThumb to resize all images in the content divs of my pages when they are served up to mobile devices.
So in my page templates for mobile devices I need to change all <img> tags in <div id="content> in the following manner:
<img src="/images/image_01.jpg">
-> <a href="/images/image_01.gif">
<img src="phpThumb.php?src=images/image_01.jpg&w=300">
</a>
<img src="/images/image_02.gif">
-> <a href="/images/image_02.gif">
<img src="phpThumb.php?src=images/image_02.gif&w=300">
</a>
What is the best way to do this?
Use str_replace to replace the old img tags to new ones. Alternatively, you can use preg_replace too if you are comfortable with regular expressions.
Since these are template pages specific to mobile devices I would suggest applying this to the actual template file instead of running some PHP code to transform it each time a mobile device requests a page. Most editors have Search & Replace functionality. And some support Regexes. Utilize this to replace all /src="([^"]+)"/ with src="phpThumb.php?src=$1&w=300".
Regular expressions are perfectly fine for that purpose. But you can also use "server-side jQuery" with http://querypath.org/ to transform it like this:
$html = qp($html);
foreach ($html->find("img") as $img) {
$src = $img->attr("src");
$img->attr("src", "new.php..."); // redirect to thumbNail
$img->replaceWith("<a href='$src'>" . $img->html() . "</a>");
}
print $html->writeHTML();
I am calling my makewindows function from a PHP file like so:
echo "Click for full description </p>";
This generates correct html, which results in a popupwindow containing the html from $html. For example(most of the html has been snipped):
Click for more
Now, I want to make an image above the link clickable, to display an image instead of html, using the same method.
I made an $imagehtml variable in php like so:
$imagehtml = "<img src='".$imageSrc."' >";
$imageSrc is the result of another method, but is always without fail a valid url for an image
passing $imageHtml to makewindows should work(the fact that it is not standards complaint html is irelivant, at least as to why what I am trying is failing. The same single line of html in a standalone html file displays in every browser fine.)
this results in the following html:
<a href="#" onclick="makewindows(<img src='removed.jpg' >); return false;">
<img src="removed.jpg" width="250" height="250"></a>
This completely fails. It has nothing to do with the image path, as no window is created at all. All I am doing is changing the variable passed, surely the window should still be created, regardless of the contents of the html?
this fails even if trying to pass about:blank, for example defining imagehtml as follows:
$imagehtml = "about:blank";
results in:
Click for full description
yet still no window.
You want something like this:
$str = '<img src="...">';
echo '<a href="#" onclick="makewindows('
. htmlspecialchars(json_encode($str))
. '); return false;">Click for full description</a></p>';
This will do the correct encoding / escaping.
You’re probably doing something wrong.
Let’s say you want to pass this HTML to a JavaScript function call inside an HTML attribute:
<img src="removed.jpg" width="250" height="250">
Then doing this is enough:
$html = '<img src="removed.jpg" width="250" height="250">';
echo 'Click for full description</p>';
The var_export function converts the value into an (PHP) equivalent expression and the htmlspecialchars function finally converts the HTML meta characters into character references.
This might not be the most elegant way, but it works.