Parsing HTML to PHP - php

I hope you can help me with the following, i have a php method to scan a directory with pictures:
<?php
$dirname = "images/";
$images = scandir($dirname);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<li> class=\"portfolio-item-preview\" <img src=\"img/portfolio/portfolio-img-01.jpg\" /></li>\n ";
// echo "<li><img src=\"img.php?src=$dirname$curimg&w=300&zc=1\" alt='' /></li>\n ";
}
}
?>
I want to put the following code after the foreach loop:
<li class="one-fourth logos">
<p>
<img src="img/portfolio/portfolio-img-01.jpg" alt=" " width="210" height="145" class="portfolio-img pretty-box"/>
</p>
</li>
If I echo all the lines PHP generates all kinds of errors, could please someone help me with parsing the lines? I am just a beginner, sorry if it's very nooby:) I want this to create a class one-fourth logos for as long the folder contains images.

Just end your php code, put the HTML and reopen php.Like that:
?>
<li class="one-fourth logos">
<p>
<img src="img/portfolio/portfolio-img-01.jpg" alt=" " width="210" height="145" class="portfolio-img pretty-box"/>
</p>
</li>
<?php

Because your HTML doesn't contain single quotation marks (') you can put them around your HTML and it will be a valid string which you can echo:
echo '<li class="one-fourth logos">
<p>
<a href="images/project2/IMG_0268.jpg" class="portfolio-item-preview" data-rel="prettyPhoto">
<img src="img/portfolio/portfolio-img-01.jpg" alt=" " width="210" height="145" class="portfolio-img pretty-box">
</a>
</p>
</li>';

Related

How can I render an image (as a byte array) from PostgresSQL as an data: URI with PHP?

<a href="#" class="image">
<!--
<img class="pic-1" src="assets/images/ADC.jpg">
<img class="pic-2" src="assets/images/ADC.jpg">
-->
<?php
$byteArray = $row['book_pic'];
$imgData = base64_encode($byteArray);
$img = "<img src= 'data:image/jpeg;base64, $imgData' alt='Image' class='pic-1'/>";
echo $img;
?>
</a>
I cannot able to convert the byte array to image, what's the solution for this?

Grabbing an attribute with DOMXPath

i know there are lots of way to grabbing an attribute.
this is my html result :
<li class="result">
<a class="block_container" href="**FIRST**">
<img alt="changeable text" src="**SOME LINK**" border="0">
</a>
</li>
<li class="result">
<a class="block_container" href="**SECOND**">
<img alt="changeable text" src="**SOME LINK**" border="0">
</a>
</li>
//and many like this ...
i can grab (href) but i have many of this attribute !
i used DOMXPath query to help me choose grab first href or second href with item number :
$a = $xpath->query("//li[#class='block_container']/a");
echo $text = $a->item(**MY ITEM NUMBER**)->nodeValue;
but it doesn't work !
can you help me grab href and src with item number ?
if you want a.href
$hrefs = $xpath->query("//li/a[#class='block_container']/#href");
foreach($hrefs as $href) {
echo $href->nodeValue ."<br>\n";
}
and if you want outerHTML of image tag
$imgs = $xpath->query("//li/a[#class='block_container']/img");
foreach($imgs as $img) {
echo $dom->saveHTML($img) ."<br>\n";
}
demo on eval.in

Regular Expression or other way to get string in right format

Please help me out.. I have following string
<p>this is text before first image</p>
<p><img class="size-full wp-image-2178636" src="image1.jpg" alt="first" /> this is first caption</p>
<p>this is text before second image.</p>
<p><img src="image2.jpg" alt="second" class="size-full wp-image-2178838" /> this is second caption</p>
<p>there may be many more images</p>
and I need above string formatted as following :
<p>this is text before first image</p>
<a href="">
<figure>
<img class="size-full wp-image-2178636" src="image1.jpg" alt="first" />
<figcaption class="newcaption">
<h1>this is first caption</h1>
</figcaption>
</figure>
</a>
<p>this is text before second image.</p>
<a href="">
<figure>
<img class="size-full wp-image-2178636" src="image2.jpg" alt="first" />
<figcaption class="newcaption">
<h1>this is second caption</h1>
</figcaption>
</figure>
</a>
<p>there may be many more images</p>
Kindly help me.. how we can do that either by regular expressions or using other way. I am doing it using PHP.
Regards,
Sachin.
Although SO is not supposed to be a code-writing service here is a quick n' dirty solution that uses the DOMDocument-approach:
$html = '...'; // your input data
$input = new DOMDocument();
$input->loadHTML($html);
$ps = $input->getElementsByTagName('p');
$output = new DOMDocument();
$counter = 0;
foreach ($ps as $p) {
if ($counter%2 === 0) {
// text before image
$p_before_image = $output->createElement("p", $p->nodeValue);
$output->appendChild($p_before_image);
}
elseif ($p->hasChildNodes()) {
// image output routine
$as_input = $p->getElementsByTagName("a");
$a_output = $output->importNode($as_input->item(0));
$figure = $output->createElement("figure");
$imgs_input = $p->getElementsByTagName("img");
$img_output = $output->importNode($imgs_input->item(0));
$figure->appendChild($img_output);
$figcaption = $output->createElement("figcaption");
$figcaption->setAttribute("class", "newcaption");
$h1 = $output->createElement("h1", $p->nodeValue);
$figcaption->appendChild($h1);
$figure->appendChild($figcaption);
$a_output->appendChild($figure);
$output->appendChild($a_output);
}
else {
// Document malformed
}
$counter++;
}
print $output->saveHTML();
Note that saveHTML() will output plain old HTML. Thus, imgs won't be turned into self-closing tags. You may want to look into saveXML() if this is important to you.

Link to be displayed as imgsrc

I have a code here that outputs a image link like http://img.domain.com/2515.jpg
<?php echo IMG_URL . $code . ".jpg" ?>
But i want to make it print this entire thing <img src="http://img.domain.com/2515.jpg" alt="" title="Created by domain.com" />
How can i format that php string <?php echo IMG_URL . $code . ".jpg" ?>
to include that entire img src link?
I am trying to fit it in this html code below
<li><a class="linkInsert" data-value="<?php echo IMG_URL . $code . ".jpg" ?>">Direct Link (email & IM)</a></li>
Update:
i figured it out below with just using '
<li><a class="linkInsert" data-value='<img src="<?php echo IMG_URL . $code ?>.jpg" alt="">'>HTML Image (websites / blogs)</a></li>
Try this,
<?php echo '<img src="'.IMG_URL.$code.'.jpg" alt="" title="Created by domain.com" />' ;?>
You just have to use single quotes and double quotes alternatively.
PHP can be embedded inside HTML:
<img src="<?php echo IMG_URL . $code ?>.jpg" alt="">

Use a PHP variable in Javascript

How can I use a PHP variable inside the Javascript onclick=function?
<?php
for($i=0;$i<10;$i++){
echo '<li><div><img src='. $mobile_image_link[$i+$p] .' width="160" height="165" alt="" border="0" /></div></div></li>';
}
?>
Just concatenate the strings, and use backslashes to escape the necessary quotes:
echo '
<a href="#"
onclick="showPopUp(\''. $mobile_image_link[$i+$p].'\',
\''. $mobile_image_link[$i+$p].'\');">
<img src="'.$mobile_image_link[$i+$p].'" alt="" />
</a>'
Would result in:
<a href="#"
onclick="showPopUp('http://mylink.com/1',
'http://mylink.com/2');">
<img src="http://mylink.com/image.png" alt="" />
</a>
On a side note, please read the HTML latest specifications, setting tag style using width="", height="" and border="" is deprecated and discouraged
Simply echo php variable in javascript code:
<script type="text/javascript">
var js_variable= <?php echo $php_variable; ?>
</script>
Either use the double quotes approach (php variables are only inserted into double quote strings.
<?php for($i=0;$i<10;$i++){
echo "<li><div><img src=\"$mobile_image_link[$i+$p]\" width="160" height="165" alt="" border="0" /></div></div></li>";}
?>
or do what you already did for the img tag. You'll need to escape some quotes to make it work.
<?php for($i=0;$i<10;$i++){
echo '<li><div><img src="'.$mobile_image_link[$i+$p].'" width="160" height="165" alt="" border="0" /></div></div></li>';}
?>
<?php
for($i=0;$i<10;$i++){
$onclickvariable = "showPopUp('".$mobile_image_link[$i+$p]."','".$mobile_image_link[$i+$p]."');"
echo '<li><div><a href="#" onclick="'.$onclickvariable.'">
<img src='.$mobile_image_link[$i+$p].' width="160" height="165" alt="" border="0" /></a>
</div></li>';}
?>

Categories