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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ltrim issue 4

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
Hi All

I need to strip the root folder name and surrounding slashes.
I had thought that specifying the string to remove would do the trick and it seemed to with the code below.

$mypath = "/root/myfolder/file.htm";
$filepath = ltrim( $mypath, "/root/" );

But if the path were this:
$mypath = "/root/rodent/file.htm"

then /root/ro gets stripped and my path ends up:
dent/file.htm.

So apparently it does not match the entire string as a whole but parts of it.

Anyone have a better way to strip the value?
Ultimately the value "/root/" will actually be a variable so the name of the root folder can be set globally elsewhere.


At my age I still learn something new every day, but I forget two others.
 
If you don;t need the slashes, and the Root will always be the first thing after the first slash you can explode the path and then use the first item in the array it generates:

Code:
$mypath="/root/subfolder/myfile.htm"
$patharray=explode("/",$mypath);
$root=$patharray[1];
echo $root;

this should echo the root portion without the slashes.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
It's not the root that I want it is the remaining path.
I already do explode the string for other reasons but I figured it would be easier to strip the root off the whole string than to loop through the array and rebuild just the portion of the path I wanted.

I can look into other string functions to see what the php equivalent of right() would be and use the length of the root path name to determine how many characters I want.
Just thought that ltrim would do the job easily.


At my age I still learn something new every day, but I forget two others.
 
ltrim() doesn't work the way you think it does.

The second parameter (in your case, "/root/") is not a string that is to be removed, but rather a set of characters. So ltrim() removes form the front of your string every "/", "r", "o" and "t" from the beginning of the string, not the literal string "/root/".

One way to remove "/root/" from "/root/myfolder/file.htm" is through a regular expression:

$filepath = preg_replace('@^/root/@', '', $mypath);





Want the best answers? Ask the best questions! TANSTAAFL!
 
Try this :

Code:
<?php
$mypath = "/root/rodent/file.htm";
$parts = explode("/", $mypath);
for($i=2;$i<count($parts);$i++)
  $filepath .= $parts[$i]."/";
$filepath = rtrim($filepath, "/");
echo $filepath;
?>

That will give you [tt]rodent/file.htm[/tt]

Regards
 
sleipnir214, how would I use the regular expression if I were to use a variable in place of the actual text?

For instance:
$rootpath = "/root/";

If the root path ever changes I do not want them to have to do a lot of searching to find out where to change it and I want to set it as a variable in the main script file.

Thanks.

At my age I still learn something new every day, but I forget two others.
 
All you need to do is get the substring from after the root length to the end. Of course that will include the filename. but taking from my previous example:

Code:
$mypath="/root/subfolder/myfile.htm";
$patharray=explode("/",$mypath);
$root=$patharray[1];
$restofpath=substr($mypath,strlen($root)+1);
echo $restofpath;


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I think vacunita is correct in not using regular expressions of not necessary.

But how complicated your code is depends on what you're removing. vacunita's code removes whatever is between the first pair of slashes.

Simply:

$remove = '/root/';
$restofpath=substr($mypath,strlen($remove)+1);

is good if you are guaranteed that mypath will always begin with '/root/' and if all you want to remove is '/root/'.


Using a regular expression to go it might read:

$remove = '/root'/;
$restofpath = preg_replace ('@^' . $remove . '@', '', $mypath);

which might be useful if you aren't guaranteed that the string begins with '/root/'.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Then try :
Code:
<?php
$root = "/root/";

$mypath = "/root/rodent/file.htm";

$filepath = str_replace($root, "", $mypath);
?>

Regards
 
dkdude:
Your code, unfortunately, does not limit removals to the beginning of the string. If $mypath is "/root/rodent/root/file.htm", your script will set $filepath to "rodentfile.htm".

I don't know how big a problem this will be for theniteowl



Want the best answers? Ask the best questions! TANSTAAFL!
 
I just tested it on PHP 4.2.2 on apache2/windows, and get the result [tt]rodent/file.htm[/tt]

Could this be platform dependant then?

Regards
 
Just noticed that PHP 5 has a fourth parameter available : count...

So maybe if we modify to ...

Code:
<?php
$root = "/root/";

$mypath = "/root/rodent/file.htm";

$filepath = str_replace($root, "", $mypath, [COLOR=red]1[/color]);
?>

... it'll work?

Regards
 
only because everyone seems to be getting involved in this thread (and it's late in my neck of the woods), how about a strpos solution that cuts off everything from the first non root slash
Code:
<?
$path = "/root/rodent/file.htm";
$pos = 0;
do {
	$pos = strpos($path,"/",$pos+1);
	}while ($pos == 0);
print substr($path,$pos+1);
?>

but i think sleipnir214's substr solution probably does what theniteowl is actually asking for
 
Thanks everyone for the help.
I think sleipnir214's solution will work the best in my situation with the one change that I had to remove the +1 as it was lopping off the first character of the next word.
So substr does not count the first character position as 0?

It works well now though.



At my age I still learn something new every day, but I forget two others.
 
So substr does not count the first character position as 0?

nope, it does.

but the strlen was already (in effect) incrementing the start position in substr because it is a count (and thus 1 based) rather than an index (effectively zero based)
 
btw, in my code above it would be better to preset $pos to -1.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top