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!

split filename off end of string 4

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
Hi All,
this should be fairly easy.

I need to drop the filename off the end of a path but cannot rely on there always being a filename so it should only drop text at the end if there is an extension to the text.

For example
/myfolder/subfolder/myfile.htm
should get stripped to
/myfolder/subfolder

but
/myfolder/subfolder/folder2
should not get stripped because it has no extension.

I guess it's a case of if there is a period then it should clip the string at the last slash?
Anyone good with regular expressions?

I could explode the path then build a new string but it just seems inefficient where I think it can be done in one or two lines without looping.

Thanks.

At my age I still learn something new every day, but I forget two others.
 
i think pathinfo is a good start. but it does not guarantee an answer.

for example
Code:
<? 
echo "<pre>";
print_r(pathinfo("/path/to/domain.com/directory/"));
?>
returns
Code:
Array
(
    [dirname] => /path/to/domain.com
    [basename] => directory
)

which is not what the OP wants.

so i think the OP needs to use pathinfo with a combination of is_file() and is_dir() to be definitive. but this, of course, requires that the path which is to be parsed is available to php. if not, then you're back to best guesses.
 
Yeah, you can do it with a regular expression. But you can also quite easily use pathinfo().

If the key 'element' does not exist in the return from pathinfo(), you know there was no file extension in the text after the last "/". In such case, add a "/" and 'basename' element back on, otherwise just return the 'dirname' element.

Here's code that gets the values both wasy (pathinfo and regexp). In keeping with the recommendations of the manual that we use regular expressions sparingly, use pathinfo().

Code:
<?php
$a_array = array ('/myfolder/subfolder/myfile.htm', '/myfolder/subfolder/folder2');

foreach ($a_array as $a)
{
	$b = pathinfo($a);
	if (isset ($b['extension']))
	{
		$c = $b['dirname'];
	}
	else
	{
		$c = $b['dirname'] . '/' . $b['basename'];
	}

	$d = preg_replace ('@/[^/]*\.[^/]*@', '', $a);

	print 'Original:  ' . $a . '<br>';
	print 'pathinfo:  ' . $c . '<br>';
	print 'regexp:    ' . $d . '<br>';
	print '<br>';
}

?>



Want the best answers? Ask the best questions! TANSTAAFL!
 
I fail to see why he needs more than pathinfo...

Use pathinfo, check that the value of the returned array is not null at key 'extension', and if it's not then basename is what he wants.

And I'm hoping the OP understands that the reason pathinfo returns what it does is that extensions are not necessary for files.

But hey, why should we let the OP read the pathinfo() description and see if he can figure it out when there's a complete code solution to be posted so he doesn't have to think.
 
Thanks everyone for the suggestions and examples.

I am curious, why is it recommended to minimize usage of regular expressions? Are they particularly slow in performance? I would have thought that the for each loop would be more process intensive than the regular expression which is why I was looking in that direction in the first place.

In my case I have already retrieved the url and it is stored as a variable. My template system redirects the request to my script page with the original URL passed as a variable.
My problem is that pages render with the root folder being the active folder and the requested page is included into the template. As a result any links contained in the content page cannot be relative links from that files location, they have to be full links from the root.
I am working to alter the active folder as the page is included so the clients can continue to use relative links when they create their content pages.
My question in this post is just a portion of what I need to get the other portion going, I just do not want to confuse the issue asking too complex a question.

I am a relative newbie at PHP and I read up on whatever I can to learn what I need for a specific task but I also have A.D.D. so extensive reading on the subject is tremendously difficult. I learn mostly by disecting code to see how it works. So feel free to throw code at me, I am learning from it not just plugging it into my own pages and forgetting about it like a lot of people would and I first try to figure something out on my own and then only ask questions about the small parts that hold me up and not ask for a whole solution.

Thanks again everyone.


At my age I still learn something new every day, but I forget two others.
 
could you solve this with html? adding a <base> tag into the <head> (which could be dynamic)? then your users could use relative paths. or could you load the images dynamically using an ajax interaction?

these architectural problems are not unusual in template systems. there is a balance between usability and idiot-proofing that is often a difficult line to tread...
 
I have it working adding a <base> tag in. I was just trying to work out a reliable and efficient way to strip the current folder path without the filename.
I built the base tag by inserting the domain and then appending on the requested local URL minus the filename.

I was attempting to use chdir() but that only changes the working folder and has not effect on the output of the page or it's href's.


At my age I still learn something new every day, but I forget two others.
 
why is it recommended to minimize usage of regular expressions? Are they particularly slow in performance? I would have thought that the for each loop would be more process intensive than the regular expression which is why I was looking in that direction in the first place.
Regular expression engines are contra-recommended except when necessary because they require quite a lot of resources. Think about it: is it easier for you to find all the "/" in a string, or is it easier for you to find all the existing matches for a description of a substring you need to find?

I don't know the exact implementation of PHP's regular expression systems, but regular expression parsing requires lots of looping, stacks, etc, to keep track of what you're matching.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top