Grabbing just the subfolder and filename from current page - php

I couldn't find another questions with the exact specs I'm needing here. So I'm posting this.
I need to use php to grab the url of the current page.
http//example.com/mysubfolder/filename.php
I need it to print out:
mysubfolder/filename
And if the filename.php has a get formula ?action=move or ?id=798 I would simply need to strip those portions off. The farthest I've seen in manuals will help to subtract only a specific portion of the .?action=doesntchangeathing
Most of what I have seen includes just the filename or a complete list of subfolders, not just one and would be using an absolute path rather than the url.
Anyone have any ideas?

You can use $_SERVER['PHP_SELF'] and cut filename extension and leading slash, so resulted expression may look something like this:
$path = implode('/', [trim(dirname($_SERVER['PHP_SELF']), '/'), pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME)]);

Related

How to check if a given URL string ends with "/index.*" or simply "/" and remove it, in PHP?

I'm trying to check for /index.[ php | htm | html | asp ], etc. (basically any wildcard suffix)
or simply "/"
to convert a canonical URL such as http://www.example.com/index.php or http://www.example.com/ to just http://www.example.com so I can just use one consistent PHP variable in my canonicalmeta tag throughout all the pages on my site.
I dont want the script to effect URLs that are NOT homepages such as http://www.example.com/page.php
REGEX preferred, but not necessary.
I'm working from the variable $current_Webpage_Complete_URL_Address in "domain_info.php" from this script: http://www.perfecterrordocs.com
I want to use just one PHP variable, formed from modifying $current_Webpage_Complete_URL_Address, if that makes any sense.
Please ask for further clarification, if neccassary.
Edit: Also, I want the newly formed variable to be named
$current_Webpage_Canonical_URL_Address
Edit(2): I just ran into another problem. Even when I do find a match with preg_match how do I remove that particular ending sub-string?
Final Answer (Works Perfectly!!):
$current_Webpage_Canonical_URL_Address = preg_replace('/((\\index)\.[a-z]+)$/', '', $current_Webpage_Complete_URL_Address);
$current_Webpage_Canonical_URL_Address = preg_replace('/\/$/', '', $current_Webpage_Canonical_URL_Address);

Upper or Lower Case Issue using get_file_contents php

I am using a PHP GET method to grab a file name that then is placed in a get_file_contents command. If it is possible, I would like to ignore letter case so that my URL's are cleaner.
For instance, example.com/file.php?n=File-Name will work but example.com/file.php?n=file-name will not work using the code below. I feel like this should be easy but I'm coming up dry. Any thoughts?
$file = $_GET['n'];
$file_content = file_get_contents($file);
Lowercase all your filenames and use:
file_get_contents(strtolower($file));
(I hope you're aware of some of the risks involved in using this.)
The Linux filesystem is case sensitive. If you want to do case insensitive matching against files that already exist on the user's machine, your only option is to obtain a directory listing and do case-insensitive comparison.
But you don't explain where the download URLs come from. If you already know the correct filenames and you want to generate prettier URLs, you can keep a list of the true pathnames and look them up when you receive a case-normalized one in a URL (you could even rename them completely, obfuscate, etc.)

using substring to add word to string

Not sure if the title for this is correct but here's my problem:
I have a table that hold image's url like so:
img/folder/imagename.jpg
Now I've created a thumbnail for each image in each folder, so if I want to display them I do a loop in my table and return all the url's but I need to add the word "thumbs" after "folder/" and before the "imagename.jpg"...Now because obviously the "folder" and "imagename" names differ in length then I can't do a count..the only thing I can figure is to look up that last "/" character and insert there and add on the "imagename.jpg" after it so end result would look like:
img/folder/thumbs/imagename.jpg
Just replace the path with a "deeper" path:
$path = str_replace('img/folder/', 'img/folder/thumbs/', $path);
There are lots of other ways to do this, but IMHO this one is perfectly clear on what it does. Theoretically it won't work universally (if your path contains multiple occurreces of img/folder/ for some reason), but let's just not go there.
you can also
$pathparts = explode("/",$path);
and then use $pathparts array to construct your path again. You would use it in cases you want to have more control over manipulating paths, but in heavy loads it's not very efficient.
echo $pathparts[0]."/".$pathparts[1]."/thumbs/".$pathparts[2];
//adition
And why not to update your database and script only save imagename.jpg
and then in the beginning of calling script define
define("IMGPATH", "img/folder/");
define("THUMBPATH", "img/folder/thumbs/");
and then call it
<?= IMGPATH."imagename.jpg" ?>
<?= THUMBPATH."imagename.jpg" ?>

PHP check if href is a file

I am using a script to check links on a given page. I am using simple html DOM to parse the information into an array. I have to check the href of all the a tags to find if they contain a file or something like # or JS.
I tried the following without success.
if(preg_match("|^(.*)|iU", $href)){
save_link();
}
I dont know it my pattern is wrong or if there is a better method to complete this function.
I want to be able to detect if $href contains .com .php .file extensions. This way it will filter out items like # "function()" and other items used in the href attribute.
EDIT:
parse_url will not work stop posting it. The value # returns as a valid url like I stated above I am trying to look for any string followed by .* with no more than 4 chars following the .
I believe that the function you're looking for is parse_url().
This function will take a URL string, and return an array of components, which will allow you to work out what kind of URL it is.
However note that it has issues with incomplete URLs in PHP versions prior to 5.4.7, so you need to have the very latest PHP to get the best out of it.
Hope that helps.
See http://php.net/manual/en/function.parse-url.php
I'm assuming you don't want to match fragments (#) because you are not concerned with following internal anchors.
parse_url breaks up the different parts of the url into an array. You can see the path component of the URL in this array and run your check against that.
You can use parse_url() , like this :
$res = parse_url($href);
if ( $res['scheme'] == 'http' || $res['scheme'] == 'https'){
//valid url
save_link();
}
UPDATE:
I've added code to filter only http and https urls, thanks to Baba for spotting this.

Change url variable using php

Say I have a url like this in a php variable:
$url = "http://mywebsite.extension/names/level/etc/page/x";
how would I automatically remove everything after the .com (or other extension) and before /page/2?
Basically I would like every url that could be in $url to become http://mywebsite.extension/page/x
Is there a way to do this in php? :s
thanks for your help guys!
I think parse_url() is the function you're looking for. You can use it to break down an URL into it's component parts, and then put it back together however you want, adding in your own things as needed.
As PeeHaa noted, explode() will be useful for dividing up the path.

Categories