Jump to content

problem with printing only part of changing string


linuxfreakphp

Recommended Posts

hey guys, i'm try to print part of the string only and i'm trying with substr and strripos and stripos.

<!DOCTYPE html>
<html>
<body>
<?php
$str= '/lalala/temp/index.html';
echo 'html = ' . strripos($str, "html");
echo '<br />';
echo 'html = ' . substr($str, stripos($str,"temp/") +5);
echo '<br />';
echo 'html = ' . substr($str, stripos($str,"temp/") +5, strripos($str,"html") -1 );
?> 
</body>
</html>

 

that output is:

 

html = 19
html = index.html
html = index.html

 

what i'm trying to get is  getting the file name of any file one once with the *.html and the second time without the ".html", but without the '/lalala/temp/'. i'm not trying to cancel sub folders path and i want to it in the output.

let say i have the next two files:

'/lalala/temp/index.html' - one-> 'index.html' two->index

'/lalala/temp/news/index.html' - one-> 'news/index.html' two-> 'news/index'

 

i also tried in the last line of the php this line:

echo 'html = ' . substr($str, stripos($str,"temp/") +5, strripos($str,".") -1 );

as you all can see i'm looking here for the "." of the file but the output is the same:

 

html = 19
html = index.html
html = index.html

 

what am i doing wrong exactly and why my first command can find the 'html' text and the two other can't?

For the filename, with or without extension, basename.

 

For the path relative to /lalala/temp,

substr($str, strlen("/lalala/temp/"))

thanks for the answer  but is what is taking less resources strlen or strripos/stripos in this case?

 

it is still don't answer my question case number two:

 

'/lalala/temp/index.html' - one-> 'index.html' two->index

'/lalala/temp/news/index.html' - one-> 'news/index.html' two-> 'news/index' (case number two)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.