This is the code which loads images with different Prefix and the prefix is printed under every image.
I need Prefix as a Title of every set of images as Category of Images
CODE :
$images = glob($dirname . "*.jpg");
foreach ($images as $image) {
if (strpos($image, '#') !== false) {
} else {
?>
<li><?php $mn_img = str_replace("/thumbs", "", $image); ?>
<div class="th [radius]">
<a href="<?php echo $mn_img ?>"><img align="middle"
src="<?php echo $image; ?>"> </a>
</div>
<p style="text-align: center">
<?php
$str = $mn_img;
$s = end(explode("/", $str));
$e = explode(".", $s);
$n = explode('-', $e[0]);
$nm = $n[0];
if ($nm === 'non') {
echo 'general';
} else {
$title = str_replace("_", " ", $nm);
echo $title;
}
?>
</p>
</li>
<?php }
}
I expect the possibility :)
MY Prefix Format is
Manager_fileoriginalname_random.jpg
Manager_fileoriginalname_random.jpg
Manager_fileoriginalname_random.jpg
Manager_fileoriginalname_random.jpg
Prefix is "Manager"
Matketing_fileoriginalname_random.jpg
Matketing_fileoriginalname_random.jpg
Matketing_fileoriginalname_random.jpg
Matketing_fileoriginalname_random.jpg
Prefix is "Marketing"
is the preg_match function is what you want :
$returnValue = preg_match( '/(.*)_.*_.*$/', 'Manager_fileoriginalname_random.jpg', $matches );
in $matches[1] you have the prefix, you just need to remplace 'Manager_fileoriginalname_random.jpg' by your var $mn_img
If i can make and advise, can use parse_url() to extract url components : http://php.net/manual/fr/function.parse-url.php
Related
I try to dynamically populate an HTML page with 2 images, the first a full image and the second a reduced image, but as I can not do AND with a foreach.
In fact, the code is ok, but I want populate my HTML with twos pics of the same folder; but this code bring back all pictures, it doesn't no the diff between the Full image and the Thumbnail.
The script bring back all picture. My folder contain images with specific titles:
Full Image = *.jpeg
Thumbnail = *_Low.jpeg
I would like to be able to modify my code to insert the full in the first line and the thumbnail in the second line.
<?php
$dir = './style/images/art/mairie/';
$files = scandir($dir);
$images = array();
array().sort();
$nb = 1;
foreach($files as $file) {
if(fnmatch('*.jpg',$file)) {
$images[] = $file;
}
}
var_dump($images);
foreach ($images as $image) {
echo '<div class="cbp-item">'.'<a class="cbp-caption fancybox-media" data-rel="portfolio" href="style/images/art/mairie/'.$image.'">'."\n"
.'<div class="cbp-caption-defaultWrap">'.'<img src="style/images/art/mairie/'.$image.'" alt="" /> </div>'."\n"
.'<div class="cbp-caption-activeWrap">'."\n"
.'<div class="cbp-l-caption-alignCenter">'."\n"
.'<div class="cbp-l-caption-body">'."\n"
.'<div class="cbp-l-caption-title"><span class="cbp-plus">'.'</span></div>'."\n"
.'</div>'."\n"
.'</div>'."\n"
.'</div>'."\n"
.'<!--/.cbp-caption-activeWrap --> '."\n"
.'</a> </div>'."\n";
}
?>
for the second line I would like to bring back the reduced photo, so to have
<div class="cbp-caption-defaultWrap"><img
src="style/images/art/mairie/Maurine_Tric-7399_Low.jpg" alt="" />
</div>
$images
Part of my $images array would look like this:
$images = [
"0" => "Maurine_Tric-7399.jpg",
"1" => "Maurine_Tric-7399_Low.jpg",
"2" => "Maurine_Tric-7407.jpg",
"3" => "Maurine_Tric-7407_Low.jpg",
"4" => "Maurine_Tric-7414.jpg",
"5" => "Maurine_Tric-7414_Low.jpg",
];
Desired Output
I'm trying to add one of the URLs in my array with large images, and the other with its thumbnail which are being differentiated with _Low:
<div class="cbp-item"><a class="cbp-caption fancybox-media" data-rel="portfolio" href="style/images/art/mairie/Maurine_Tric-7399.jpg"> <div class="cbp-caption-defaultWrap"><img src="style/images/art/mairie/Maurine_Tric-7399_Low.jpg" alt="" /> </div>
Just use preg_match() instead of fnmatch(), matching only the thumbail images and marking the part before _Low.jpg as Subpattern.
Then you can easily construct the filename of both images from that stub:
<?php
$dir = './style/images/art/mairie/';
$files = scandir($dir);
$images = array();
array().sort();
$matches = NULL;
foreach ($files as $file) {
if (preg_match('/^(.+)_Low\.jpg$/', $file, $matches)) {
$images[] = $matches[1];
}
}
foreach ($images as $image) {
echo '<div class="cbp-item"><a class="cbp-caption fancybox-media" data-rel="portfolio" href="' . $dir . $image . '.jpg"> <div class="cbp-caption-defaultWrap"><img src="' . $dir . $image . '_Low.jpg" alt="" /></div>';
}
?>
If we wish to just add one URL for our images and one for their thumbs in the foreach, we might be able to define a $html variable and step by step add to it, then we would finally echo that, and our code would look like something similar to:
<?php
$html = '<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document sans titre</title>
</head>
<body>';
$html .= '<ul>';
$dir = './style/images/art/mairie/';
$files = scandir($dir);
$images = array();
$nb = 1;
foreach ($files as $file) {
if (fnmatch('*.jpg', $file)) {
$images[] = $file;
}
}
foreach ($images as $key => $image) {
if ($key % 2 == 0) {
$html .= '<div class="cbp-item">
<a class="cbp-caption fancybox-media" data-rel="portfolio" href="style/images/art/mairie/' . $image . '">
<div class="cbp-caption-defaultWrap"><img src="style/images/art/mairie/' . $images[$key + 1] . '" alt="" /> </div>
<div class="cbp-caption-activeWrap">
<div class="cbp-l-caption-alignCenter">
<div class="cbp-l-caption-body">
<div class="cbp-l-caption-title"><span class="cbp-plus"></span></div>
</div>
</div>
</div>
</a>
</div>';
}
}
$html .= '</ul></body></html>';
echo $html;
Modifications
What we are modifying here is that we add an if to run every other time.
if ($key % 2 == 0) {
}
We add $key var in our foreach and finally we have two image links which we replace one of them with $images[$key+1], which one of them is for thumbs:
$image
$images[$key+1]
I am using this code for explode and show GET variables. But I would like remove current query in the link:
My explode code:
$k = $_GET['sef'];
$s_explode = explode("-",$k);
foreach($s_explode as $q) {
if($q==$s_explode[0]) {
echo '<a class="active" href="/category/'.$q.'">'.$s_explode[0].' <span class="dismiss">×</span></a>';
} else {
echo ''.$q.' <span class="dismiss">×</span>';
}
}
If I using GET
website.com/?sef=game-book-video
Print is:
<a class="active" href="/category/game">game</a>
book
video
I would like if I using GET
website.com/?sef=game-book-video
<a class="active" href="/category/book-video">game</a>
book
video
I hope I can explain good sorry for my bad English.
Your code would be like this:
$k = $_GET['sef'];
$s_explode = explode("-", $k);
//game-book-video
foreach($s_explode as $i => $q) {
$parts = $s_explode;
if(($key = array_search($q, $parts)) !== false) {
unset($parts[$key]);
}
$class = ($i == 0 ? "class='active'" : '');
echo '<a ' . $class . ' href="/category/'.implode('-', $parts).'">'.$q.' <span class="dismiss">×</span></a>';
}
I have search before & don't find answer
http://i.stack.imgur.com/6mZRz.png
I want to get url of image after " : "
I am using simple dom html
My listing is..
include 'simple_html_dom.php';
$target = 'http://search.aol.com/aol/image?q=aku+ganteng';
$html = file_get_html($target);
foreach($html->find("div[class=inner]") as $f){
$crot = $f->find("img",0)->src;
echo '<img src="'.$crot.'"/><br/>';
}
The HTML listing
<div class="inner">
<span class="imgc"></span>
<a href="imageDetails?s_it=imageDetails&q=aku+ganteng&img=http%3A%2F%2Fsd.keepcalm-o-matic.co.uk%2Fi%2Fjarene-ibuk-ku-aku-ganteng-cok-d.png&v_t=topsearchbox.image&host=http%3A%2F%2Fwww.keepcalm-o-matic.co.uk%2Fp%2Fjarene-ibuk-ku-aku-ganteng-cok-d%2F&width=129&height=151&thumbUrl=https%3A%2F%2Fencrypted-tbn1.gstatic.com%2Fimages%3Fq%3Dtbn%3AANd9GcQD_uhCuZ6yy19yB452fbEQAabTwa3xrOyVdArDf2COKl3AKKYX30dxAht7Nw%3Asd.keepcalm-o-matic.co.uk%2Fi%2Fjarene-ibuk-ku-aku-ganteng-cok-d.png&b=image%3Fs_it%3DimageResultsBack%26v_t%3Dtopsearchbox.image%26q%3Daku%2Bganteng%26oreq%3D310738f642cd4b029e1f8c897168a385&imgHeight=700&imgWidth=600&imgTitle=JARENE+IBUK%26%2339%3BKU+AKU+GANTENG+COK&imgSize=39960&hostName=www.keepcalm-o-matic.co.uk" onclick="return sl.sl(null,null,null,this,'image_results',1)">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQD_uhCuZ6yy19yB452fbEQAabTwa3xrOyVdArDf2COKl3AKKYX30dxAht7Nw:sd.keepcalm-o-matic.co.uk/i/jarene-ibuk-ku-aku-ganteng-cok-d.png" width="129" height="151" alt="JARENE IBUK'KU AKU GANTENG COK" title="JARENE IBUK'KU AKU GANTENG COK"></a>
</div>
I want get part of this
sd.keepcalm-o-matic.co.uk/i/jarene-ibuk-ku-aku-ganteng-cok-d.png
How to get full url target?
You probably need this:
$crot= "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQD_uhCuZ6yy19yB452fbEQAabTwa3xrOyVdArDf2COKl3AKKYX30dxAht7Nw:sd.keepcalm-o-matic.co.uk/i/jarene-ibuk-ku-aku-ganteng-cok-d.png"
preg_match_all('/.*:(.*?)$/sim', $crot, $part, PREG_PATTERN_ORDER);
$part = $part[1][0];
echo $part;
Output:
sd.keepcalm-o-matic.co.uk/i/jarene-ibuk-ku-aku-ganteng-cok-d.png
Full code:
<?
include 'simple_html_dom.php';
$target = 'http://search.aol.com/aol/image?q=aku+ganteng';
$html = file_get_html($target);
foreach($html->find("div[class=inner]") as $f){
$crot = $f->find("img",0)->src;
$ahh = str_replace("thumbs","download",$crot);
$wall = str_replace("t1","1920x1080",$ahh);
preg_match_all('/.*:(.*?)$/sim', $crot, $part, PREG_PATTERN_ORDER);
$part = $part[1][0];
echo $part; //this is what you want.
echo "<a href='$crot'><img src='$crot'/></a><br/>";
}
?>
Currently my script list all folders by their name.
Now I want to sort it by date they have created.
Index.php :
<?php
include("include.php");
include("header.php");
?>
<h1>Photo</h1>
<p class="breadcrumb">home > gallery</p>
<?php if(count($categories_array)<=0){?>
<p>There are no photo categories, create one or more categories before uploading photos</p>
<?php }
if(count($categories_array)>0){?>
<div>
<?php foreach($categories_array as $photo_category=>$photos_array){?>
<?php
$category_thumbnail = $gallery_url."/layout/pixel.gif";
if(file_exists('files/'.$photo_category.'/thumbnail.jpg')){
$category_thumbnail = $gallery_url.'/'.$photo_category.'/thumbnail.jpg';
}
$category_url = $gallery_url.'/'.$photo_category;
?>
<span class="category_thumbnail_span" style="width:<?php echo $settings_thumbnail_width;?>px; height:<?php echo $settings_thumbnail_height+20;?>px;">
<a class="category_thumbnail_image" href="<?php echo $category_url;?>" style="width:<?php echo $settings_thumbnail_width;?>px; height:<?php echo $settings_thumbnail_height;?>px; background-image:url('<?php echo $gallery_url;?>/layout/lens_48x48.png');" title="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>">
<img src="<?php echo $category_thumbnail;?>" width="<?php echo $settings_thumbnail_width;?>" height="<?php echo $settings_thumbnail_height;?>" alt="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>" />
</a>
<a class="category_thumbnail_title" href="<?php echo $category_url;?>" title="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>">
<?php echo htmlentities(str_replace('-',' ', truncate_by_letters($photo_category, 16, '..')), ENT_QUOTES, "UTF-8");?> (<?php echo count($photos_array);?>)
</a>
</span>
<?php } ?>
</div>
include.php :
<?php
include("settings.php");
$page_load_start = microtime(true);
if (!isset($_SESSION)) {
session_start();
}
setlocale(LC_CTYPE, "en_US.UTF-8");
$gallery_domain = str_replace("www.", "", $_SERVER['HTTP_HOST']);
$gallery_url = dirname($_SERVER['SCRIPT_NAME']);
$gallery_url = str_replace($_SERVER['DOCUMENT_ROOT'], '', $gallery_url);
error_reporting(E_ALL ^ E_NOTICE);
include("system_functions.php");
$is_admin = false;
if($_SESSION['session_admin'] == md5($_SESSION['session_secret'].$settings_secret)){
$is_admin = true;
}
$categories_array = array();
$timer_1 = microtime(true);
// loop over files directory and read the categories
$scandir_array = scandir('files');
foreach($scandir_array as $folder){
if(is_dir('files/'.$folder) and $folder!='.' and $folder!='..'){
// define this key in the array, it will be blank, store categories as keys
$categories_array[$folder] = array(filectime($folder));
// $total_photos_array[$folder] = 0;
$files_in_dir = scandir('files/'.$folder);
foreach($files_in_dir as $file){
if($file!='.' and $file!='..'){
// if file is not the category thumbnail (thumbnail.jpg) and not _thumb.jpg and not _small.jpg
if($file != "thumbnail.jpg" and substr($file, strlen($file)-10) != "_small.jpg" and substr($file, strlen($file)-10) != "_thumb.jpg"){
// $total_photos_array[$folder]++;
$base_file_name = substr($file, 0, strlen($file)-4);
// insert this file in the array of files
array_push($categories_array[$folder], $base_file_name);
//echo "<br>$base_file_name";
}
}
}
}
}
$timer_2 = microtime(true);
// !! if you use wrong sorting parameter it will convert the category string keys into integer
arsort($categories_array);
//ksort($categories_array);
?>
I tried with $base_file_name = filemtime($file); but does not seems to work.
Any help in this regards will be appreciated.
Thanks in advance.
You could write function, similar to this. It's not tested, so it proboly won't work right away.
$scandir_array = order_by_date(scandir('files'));
function order_by_date($files) {
$return = array();
foreach ($files as $file) {
$return[$file] = filemtime($file);
}
arsort($files);
$return = array_keys($files);
return $return;
}
SOLVED:
$categories_array = array ();
$temp_array = array ();
$timer_1 = microtime ( true );
$scandir_array = scandir ( 'files' );
foreach ( $scandir_array as $folder ) {
if (is_dir ( 'files/' . $folder ) and $folder != '.' and $folder != '..') {
$timestamp = filemtime ( 'files/' . $folder );
$temp_array[$timestamp] = $folder;
}
}
krsort ( $temp_array ); // sorts an array by key.
foreach ( $temp_array as $folder ) {
// define this key in the array, it will be blank, store categories as keys
$categories_array [$folder] = array ();
$files_in_dir = scandir ( 'files/' . $folder );
foreach ( $files_in_dir as $file ) {
if ($file != '.' and $file != '..') {
// if file is not the category thumbnail (thumbnail.jpg) and not _thumb.jpg and not _small.jpg
if ($file != "thumbnail.jpg" and substr ( $file, strlen ( $file ) - 10 ) != "_small.jpg" and substr ( $file, strlen ( $file ) - 10 ) != "_thumb.jpg") {
$base_file_name = substr ( $file, 0, strlen ( $file ) - 4 );
// insert this file in the array of files
array_push ( $categories_array [$folder], $base_file_name );
} // if
} // if
} // foreach
} // foreach
I want to separate words from 1 line.
I tried this with my following code:
$tags = 'why,what,or,too,';
preg_match_all ("/,(.*),/U", $tags, $pat_array);
print $pat_array[0][0]." <br> ".$pat_array[0][1]."\n";
I want the result to resemble:
<img src="why.jpg"></br>
<img src="what.jpg"</br>
<img src="or.jpg"</br>
<img src="too.jpg"
I want to do like this site when you write a question you have to write 'Tags'.
<?
$tags = 'why,what,or,too,';
$words = explode(',', $tags);
?>
<?php foreach($words as $word) {
if(!empty($word))?>
<img src="<?php echo $word;?>.jpg"></br>
<?php } ?>
after exploding you will have an array
$words[0] = 'why';
$words[1] = 'what';
$words[2] = 'or';
$words[3] = 'too';
$words[4] = '';
Use the explode function to split the input string by a given delimiter:
$tags = 'why,what,or,too,';
$array = explode(",", $tags);
Then iterate the array to display each tag:
foreach($array as $tag) {
if(!empty($tag)) {
echo "<img src=\"$tag.jpg\"></br>";
}
}
$tags = 'why,what,or,too,';
$temp = explode(",", $tags); // will return you array
foreach($temp as $tag) {
if(!empty($tag)
echo "<img src=\"$tag.jpg\"></br>";
}
Easy with explode
$tags = 'why,what,or,too,';
$array = explode(',',$tags );
echo '<pre>';
print_R($array);
<img src="<?php echo $array[0]?>"></br>
<img src="<?php echo $array[1]?>"></br>
<img src="<?php echo $array[2]?>"></br>
<img src="<?php echo $array[3]?>">
use explode for this like it didn't print empty tag
$tags = 'why,what,or,too,';
$array=explode(",",$tags);
$buf=array();
foreach($array as $tag) {
if(empty($tag))continue;
$buf[]="<img src=\"$tag.jpg\">";
}
echo implode('</br>',$buf);
output
<img src="why.jpg"></br>
<img src="what.jpg"></br>
<img src="or.jpg"></br>
<img src="too.jpg">