Hi guyz, Need a little advice, I'm a php novice... I'm trying to get a text from a filename to use it in the title of the page. So I have file XXXXXXXX_XX_XXXXXX_text-I-want-to-grab.jpg So parts of the filename I get like this: $month = substr($Files[$pg], 4, 2); $day = substr($Files[$pg], 6, 2); $year = substr($Files[$pg], 0, 4); $photoname = substr($Files[$pg], 19, 4); photoname is the last part, but I don't want only 4 characters but all the text. How do I do this with substr?
yes $photoname = substr($Files[$pg], 19); like this, it works, but I also get the filename ending like .jpg how do I go about removing that?
hey, I tried that, $photoname = substr($Files[$pg], 19, (strlen ($Files)) - (strlen (strrchr($Files,'.')))); but this seems to give me only last characters irrespective of the file name, you can check it here: portfolio.sandrophoto.com/street-photography/ any ideas what is going bad? thanks Kaimi btw, ur really helpful
If you're using it only for photos with static extension then define static offset from end of filename. Or you can use something like that $str = 'XXXXXXXX_XX_XXXXXX_text-I-want-to-grab.jpg'; print basename($str, ".jpg"); Code (markup): or $str = 'XXXXXXXX_XX_XXXXXX_text-I-want-to-grab.jpg'; print substr($str, 19, -4); Code (markup): or $str = 'XXXXXXXX_XX_XXXXXX_text-I-want-to-grab.jpg'; print substr($str, 19, strpos($str, '.') - 19); Code (markup):
A little method I like to use when grabbing file names that follow some pattern, modified for your code snippet $photoname = end(explode('_', $Files[$pg])); // you will get text-I-want-to-grab.jpg, you can strip it out like this $photoname = substr($photoname, 0, -4); PHP: Many ways to do it, I only posted mine.
I wouldn't use substr(), I would use explode(); <?php $Files[0] = "XXXXXXXX_XX_XXXXXX_text-I-want-to-grab.jpg"; $fileParts = explode("_",$Files[0]); $month = $fileParts[0]; $day = $fileParts[1]; $year = $fileParts[2]; $photoname = $fileParts[3]; echo $photoname; ?> PHP:
kaimi spasibo ogramnoe... Gray Fox, job0107, thx allot! I'm giving +rep to everyone who helped thanks to you problem is solved! But I'm having some issues with the script, again... and I'm pretty sure problems I'm having are easy to fix with knowledge of course (which I don't have). so again I have some questions... My rss page isn't displaying title of each entry, instead it displays name of the first photo page... which seems strange! You can see it here: http://portfolio.sandrophoto.com/street-photography/rss.php and the code: This variable is misbehaving: $photonamewithoutlines I get it through: I don't get why $i changes and $photonamewithoutlines stays same across all entries... :S
First, $photonamewithoutlines does not refer to an array like $photonamewithoutlines[$i]. So where does $photonamewithoutlines get it's value from?
job, it gets value from $photoname, which in turn gets value from the file names! btw, script I'm modifying is this: http://www.tubatomic.com/revolver/
I created an array called $photonamewithoutlines. And I loaded the array in the file code.php with the file names using this command $photonamewithoutlines[] = str_replace("-", " ", "$photoname"); PHP: Then I am using that array in the for loop, in the <item><title> section in rss.php. Example code.php <?php //////// Revolver - code.php Version 1.4 ( www.loadrevolver.com ) //////// By Tubatomic Studio, llc ( www.tubatomic.com ) //////////////////////// $Files = array(); $Files[] = ""; $PGarray = array(); $dirfiles = opendir($dir); $cdate = date("Ymd"); $pg = $_GET['pg']; $playcheck = strstr($pg, '_'); if($playcheck == "_p"){ $play = true;} $pg = str_replace("_p", "", $pg); if($pg == "" and $play != true and $playdefault == true){$play = true;} if($highest_lowest == true) {$imgcount = 1; } else {$imgcount = 0;} if (! $dirfiles)die('Can Not Find Any Images' . $dir); while ($Filename = readdir($dirfiles)) { if($datedriven == true) {$fulldate = substr($Filename, 0, 8); }else{$fulldate = 0;} if ($datedriven == true AND $fulldate > $cdate)continue; if ($datedriven == true AND strlen($Filename) > 23 || $datedriven == true AND strlen($Filename) < 15)continue; $filetype = strtolower(substr($Filename, (strlen($Filename)-3), 3)); if ($filetype != "gif" and $filetype != "jpg" and $filetype != "png")continue; $Files[] = $Filename; if($highest_lowest == true) {$PGarray[$imgcount] = $imgcount; } else {$PGarray[$imgcount] = $imgcount+1;} $imgcount++; } ////////////////////////////////////////////////////// // Added code to load array $photonamewithoutlines. // for($i=count($Files)-1;$i>0;$i--) { $photoname = substr($Files[$i], 19, -4); $photonamewithoutlines[] = str_replace("-", " ", "$photoname"); } ////////////////////////////////////////////////////// if($highest_lowest == true){ sort($Files); $imgcount = $imgcount-1; if($pg == "" or $pg > $imgcount or $pg < 1) $pg = $imgcount; $nextpage = $pg -1;if($nextpage < 1){ $nextpage = $imgcount; } $prevpage = $pg +1;if($prevpage > $imgcount){ $prevpage = 1; } }else{ sort($Files); $imgcount = $imgcount; if($pg == "" or $pg > $imgcount or $pg < 1) $pg = 1; $nextpage = $pg +1;if($nextpage > $imgcount){ $nextpage = 1; } $prevpage = $pg -1;if($prevpage < 1){ $prevpage = $imgcount; } } $imagesource = $dir . $Files[$pg]; $imageinfo = getimagesize($imagesource); $imageheight = $imageinfo[1]; $imagewidth = $imageinfo[0]; if($datedriven == true) { $month = substr($Files[$pg], 4, 2); $day = substr($Files[$pg], 6, 2); $year = substr($Files[$pg], 0, 4); if(strlen($Files[$pg]) > 22) {$imagecolor = "#" . substr($Files[$pg], 12,6);} else {$imagecolor = $defaultbackcolor;} }else{ $month = ""; $day = ""; $year = ""; if(strlen($Files[$pg]) > 22 && $getbackgroundcolorfromfilename == true) {$imagecolor = "#" . substr($Files[$pg], 12,6);} else {$imagecolor = $defaultbackcolor;} } $jscript_slideshow = '<script type="text/javascript"> playstatus = "";'; $jscript_slideshow .= 'nextpage = "'.$nextpage.'";'; $delayx1000 = $delay * 1000; $jscript_slideshow.= 'delay = '.$delayx1000.';'; if($play == true){$jscript_slideshow.= 'window.onload = play();';} $jscript_slideshow.= "\n"; $jscript_slideshow.=' function play(){if(playstatus != "playing"){if(delay != 0){slideshow = window.setInterval("window.location = \'?pg="+nextpage+"_p\'",delay);playstatus = "playing";}}} function stop(){if(playstatus == "playing"){if(delay != 0){clearInterval(slideshow);playstatus = "";}}} </script> '; ?> PHP: Example rss.php <?php //////// Revolver - RSS Feed Version 1.4 ( www.loadrevolver.com ) //////// By Tubatomic Studio, llc ( www.tubatomic.com ) include 'config.php'; /// Your Settings File //////// include 'code.php'; /// The Important Stuff //////// echo '<?xml version="1.0"?> <rss version="2.0"> <channel> <title>'.$sitetitle.'</title> <link>'.$mainsiteurl.'</link> <description>'.$sitedescription.'</description> <language>'.$language.'</language> <docs>'.$mainsiteurl.'</docs> <generator>Revolver</generator> '; // Render Feed if($highest_lowest == true){ rsort($Files); rsort($PGarray);}else{sort($Files); sort($PGarray); array_unshift($PGarray, "");}; for ( $i=0; $i <= count($Files); $i++) { if($Files[$i] != null){ echo '<item> <title>'.$photonamewithoutlines[$i].' - Page '.$PGarray[$i].'</title> <link>'.$fullpath.$thisfilename.'?pg='.$PGarray[$i].'</link> <description>This is page '.$PGarray[$i].' out of '.$imgcount.' total.</description> <guid>'.$fullpath.$dir.$Files[$i].'</guid> </item> '; } } echo'</channel> </rss>'; ?> PHP: