Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Regular Expressions

Status
Not open for further replies.

felix001

Technical User
Nov 15, 2008
102
GB
Im tring to add a small part to my script which will find anything after the last . at the moment it finds anything after the first dot it finds....

$type = explode(".", $file);

so i believe i need to give it a reg expressions to find the last . in the line... ive tried a few reg expr combos but nothing as worked...

Any help would be great..
 
[1] There is nothing wrong with explode. This is how.
[tt]
$type = explode(".", $file);
$ext=$type[count($type)-1];

if (count($type)>1) {
echo "ext: ".$type[count($type)-1]."\n";
} else {
echo "ext undefined"."\n";
}
[/tt]
[2] If you want to use regex for this matter as an exercise of skill, this is how.
[tt]
$pattern="/\.([^.]+)$/";
preg_match($pattern,$file,$m);

if (count($m)!=0) {
echo "ext: ".$m[count($m)-1]."\n";
} else {
echo "ext undefined"."\n";
}
[/tt]
ps If it is php page, replace \n by <br /> accordingly.
 
Ok it might help if I show you this script....

it finds the file type by finding the first . in the title and classing all the letters after it as the file type. The problem is, is that if there are 2 dots, it will start from the first one. Ideally I want it to use the last dot


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>FileBrowser</title>

<style>

body {
font-family:Arial, Helvetica, sans-serif;
font-size:14px
}

.current_dir {
font-size:18px;
font-weight:bold
}

</style>

</head>

<body>
<center>
<div style="text-align:left; width:740px">
<div class="current_dir">
<?php
error_reporting(E_ALL);
$dir_name = explode("/", dirname(__FILE__));
if(isset($_GET['dir']) and file_exists($_GET['dir'])){
echo "./".$dir_name[count($dir_name)-1]."/".trim($_GET['dir'], "./")."/";
} else {
echo "./".$dir_name[count($dir_name)-1]."/";
}
?>
</div><br/>
<table border="1" cellspacing="0" cellpadding="2">
<tr><td width="40"><b>Type</b></td><td width="200"><b>Filename</b></td><td width="100"><b>Size</b></td><td width="300"><b>Date Modified</b></td><td width="100"><b>Permissions</b></td>
</tr>
<?php

// Settings //////////////

/* Only allow users to view the same directory this file is placed in. false: let users browse all directories */
$current_dir_only = false;

/* Hide certain file types. Example: $hide_files = array("php", "txt"); */
$hide_files = array();

//////////////////////////

if(isset($_GET['dir'])){
$dir = $_GET['dir'];
} else {
$dir = ".";
}

if($current_dir_only == true){
$dir = ".";
}

if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {

if($file == ".." and isset($_GET['dir'])){
$back = explode("/", $_GET['dir']);
unset($back[count($back)-1]);
$link = implode("/", $back);
} else {
$link = $file;
$back = array();
}

// if dir exists, make the link to it
if(is_dir("./$dir/$link/")){
$link = "$dir/$link";
}

// .. link problem
if(count($back)-1 == 0 and is_dir("./$dir/$link/")){
$link = "";
} else {
$link = "dir=$link";
}

// as not to make ../dir/../beginning
if($file == ".." and count($back) == 1){
$link = "";
}

// link to file or dir
if(is_file(trim("$dir/$file", "dir="))){
$link = trim("$dir/$file", "dir=");
} else {
$link = "?{$link}";
}

if(is_file(trim("$dir/$file", "dir="))){
$filesize = round(filesize($link)/1000, 2);
$filesize .= "KB";

$modified = date("j/m/y h:i", filemtime($link));

$type = explode(".", $file);
$type = $type[count($file)];

$perm = substr(sprintf('%o', fileperms($link)), -4);

} else {

$dirname = trim("$dir/$file", "dir=");

$filesize = "&nbsp;";
$modified = "&nbsp;";
$type = "Dir";

if(file_exists($dirname)){
$perm = substr(sprintf('%o', fileperms("$dirname")), -4);
}
}

if($link == "?dir=./"){
$link = "?dir=../";
}

if($current_dir_only == true and $type == "Dir"){
$show_dir = 0;
} else {
$show_dir = 1;
}

if(!isset($_GET['dir']) and $file == ".."){
$no_go_back = true;
} else {
$no_go_back = false;
}

if($file != "."){
if($no_go_back == false){
if($show_dir != 0){
if(!in_array($type, $hide_files)){

echo "<tr><td>$type</td><td><a href=\"$link\">$file</a></td><td>$filesize</td><td>$modified</td><td>$perm</td></tr>\n";
}
}
}
}
}

closedir($dh);
}
}


?>
</table>
</div>
<br />
<!-- Do not remove this notice -->
Powered by <a href=" &copy; Copyright 2007 Peter Bailey
<!-- Do not remove this notice -->
</center>
</body></html>
 
>Ok it might help if I show you this script....
Where is the explode you originally posted? Do we have to read all that? to answer a question?
 
$type = explode(".", $file);
$type = $type[count($file)];

 
Ok that might, work but what would the syntax be...

Im very new to PHP...
 
>$type = $type[count($file)];
What is that, count($file)? I have not shown any of the sort, if that comes from the idea in my post above - it may not, I am just not sure.
 
To be honest this script was obtained from a site. I just wanted to slighty tweak it.... here are the things that are still needed ....

Changing the fileextension to pick up the last letters after the last dot.
Putting all the files in directory in alphabetic order with dir at the top.
 
But it is an atomic question which a precise answer can be given. Why mixing up all the rest? You just have to determine what $file precisely is. If it is an array, do it for each entry. (I think that is debugging-123, no?)
[tt]
$file="abc.def.xyz";
//$file="abc";
$type = explode(".", $file);
if (count($type)>1) {
echo "file ".$file. " ext: ".$type[[blue]count($type)-1[/blue]]."<br />";
} else {
echo "file ".$file." ext is undefined"."<br />";
}[/tt]
 
This is if i change that dot to say a "T" or a "F" it will give me the file type after that letter thats why i was think of using the regluar expression for finding the last dot.
 
What on earth is an os that defines a file extension as after T or F? I respect and even practice philosophy but that's a bit too much. Suppose it is F, you can do this.
[tt]
$file="abcdeFfghikF$pattern="/[blue]F[/blue]([^[blue]F[/blue]]+)$/";
preg_match($pattern,$file,$m);

if (count($m)!=0) {
echo "file ".$file. " ext: ".$m[count($m)-1]."<br />";
} else {
echo "file ".$file." ext is undefined"."<br />";
}[/tt]
 
due to not knowing php it would be great if I could just use a regular expression within the explode function, or can i not do that....

My previous example with T and F were mearly just examples, when testing.
 
Ok, maybe a different approach would be to use the strrchr function !!!
 
@felix001

to get the characters after the last dot you can do this

Code:
$fParts = explode('.', $filename);
$extension = end($fParts);

but this presupposes that there are dots in the file name. there need not be. to add more granularity you might do this
Code:
$fParts = explode('.', $filename);
if (count($fParts) === 1){
 echo 'no extension';
} else {
 $extension = end($fParts);
 echo 'extension is ' . $extension;
}

all of the above is programmatic and there are many edge cases that you have to test for in order to develop a solid solution. As pointed out above, pathinfo may be better as the code underlying this function already handles many edge cases.
Code:
$extension = pathinfo ($file, PATHINFO_BASENAME);

you can find more information about pathinfo here:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top