Php remove domain extension

Remove filename and extension name and only show folder name

Solution 1: To get the host part of a URL use : And for the rest see my answer to Remove domain extension . Solution 2: Could you use string replace?

Remove filename and extension name and only show folder name

To remove the slash, you must first disable DirectorySlash

DirectorySlash Off DirectoryIndex disabled 

Direct access to PHP files can be answered with a R=404 status code

RewriteCond % ^$ RewriteRule \.php$ - [R=404,L] 

And then, you can rewrite requests pointing to a directory and containing an index.php

RewriteCond %/index.php -f RewriteRule ^.*$ /$0/index.php [L] 

This RewriteCond looks, if the requested URL is a directory and if there is an index.php in this directory. If this is the case, then the index.php is executed.

DirectorySlash Off DirectoryIndex disabled RewriteEngine on # prevent direct access to PHP files RewriteCond % ^$ RewriteRule \.php$ - [R=404,L] # rewrite requests for a directory to index.php RewriteCond %/index.php -f RewriteRule ^.*$ /$0/index.php [L] 

Htaccess doesn’t have anything common with PHP. Htaccess is Apache’s configuration file, so you need to play with htaccess to achieve that what you want. On the other hand your solution will be really dirty. Learn about MVC and FrontController to make your structure cleaner.

PHP — stripping the extension from a file name string, I want to strip the extension from a filename, and get the file name — e.g. file.xml -> file, image.jpeg -> image, test.march.txt -> test.march, etc. This …

Remove extension from image filename php

You can get the filename without the extension using pathinfo so for title=» you could use pathinfo($file, PATHINFO_FILENAME);

$file_without_ext = substr($file, 0, strrpos(".", $file)); 

For a «state-of-the-art» OO code, I suggest the following:

$files = array(); foreach (new FilesystemIterator('images/schools/') as $file) < switch (strtolower($file->getExtension())) < case 'gif': case 'jpg': case 'jpeg': case 'png': $files[] = $file; break; >> foreach ($files as $file) < echo '
  • You do not use the deprecated ereg() functions anymore.
  • You escape possible special HTML characters using htmlentities() .

Remove .php extensions with .htaccess without, Simply combining the above code with DirectoryIndex index.php does not achieve both results. Can someone help me combine these two …

Remove file extension on file upload

You can use the pathinfo() function (docs).

$example = "my_file.jpeg"; $filename = pathinfo($example, PATHINFO_FILENAME); echo $filename; // my_file 
$name = preg_replace('/(.+)\.\w+$/U', $_FILES['images']['name'], '$1'); 

You can use rename to remove the extension:

Читайте также:  75 готовых решений php

How to remove extension from string in PHP?, There are three ways of removing an extension from string. They are as follows. Using an inbuilt function pathinfo. Using an inbuilt function …

PHP Remove Domain Name Extension from String

To get the host part of a URL use parse_url :

$host = parse_url($url, PHP_URL_HOST); 

And for the rest see my answer to Remove domain extension .

Could you use string replace?

This would strip out ‘http://’ from a string. All you would have to do first is get the current url of the page and pass it through any string replace you wanted to..

I would str_replace out the ‘http://’ and then explode the periods in the full domain name.

How can I remove the .php extension from my PHP page?, According to this website: Change your .htaccess file like this: RewriteEngine On RewriteCond % !-f RewriteRule ^ ( …

Источник

Remove Domain Extension

And for the rest see my answer to Remove domain extension.

PowerShell: Remove domain extensions in email list

As Fairy points out you need to be aware of your regex meta characters like .

I would like to remove all domain extensions.

If that is the case then you should not need to be typing in each one that you want to remove. You should be able to remove all character after and including the last period.

Since -replace is an array operator you don’t need to use ForEach-Object

(Get-Content $Sourcefile) -replace "(@.+?)\..*$",'$1' | Set-Content $Output

That will match everything after and including the «@». It replaces that with just the «@» and what is before the first period.

If you really want to replace certain domains they you might be better off keeping a string array and building a regex replacement string with that. Makes it easier to make changes and the code will stay clean.

$suffixesToRemove = "com","co.uk","nl","al"
$regex = "\.($(($suffixesToRemove|ForEach-Object) -join "|"))$"
(Get-Content $Sourcefile) -replace $regex | Set-Content $Output

The computed regex string would look like this

So it uses a alternating group with meta-charaters escaped.

Removing domain extension from string using preg_replace?

You do have to escape the dot but also regular expressions have to be passed in the form of /pattern/ that is between slashes

Side note, there are better recommended ways to do this than using the almighty regular expressions. For example:

$url = explode(".","http://static.facebook.com"); 
echo $url[1]; // facebook

Regex to retrieve domain.extension from a url

You could achieve the same result with replace method but match is some how more suitable:

console.log( window.location.hostname.match(/[^\s.]+\.[^\s.]+$/)[0]);

Extract domain name with no domain extension in Google Sheets

Since you may have only .com or .co.uk at the end of the strings, you may use

Also, you may remove them at the end with

Читайте также:  Exception thrown java lang stringindexoutofboundsexception

You may also consider a bit more generic patterns like

=REGEXEXTRACT(A4, "^(.+?)(?:\.co)?\.[^.]+$")
=REGEXREPLACE(A4, "(?:\.co)?\.[^.]+$", "")

Pattern details

  • ^ — start of string
  • (.+) — 1 or more chars other than line break chars, as many as possible
  • (.+?) — 1 or more chars other than line break chars, as few as possible (needed in the more generic patterns because the subsequent pattern is optional)
  • \.(?:co\.uk|com)$ — . and then co.uk or com at the end of the string
  • (?:\.co)?\.[^.]+$ — an optional .co char sequence and then . and 1 or more chars other than a . till the end of the string.

Extract domain name without suffix or subdomain

You’re working with domain names, so you may want to use some tools that were designed to do so:

library(urltools)

df
suffix_extract(df$site)
## host subdomain domain suffix
## 1 Google.com google com
## 2 yahoo.in yahoo in
## 3 facebook.com facebook com
## 4 badge.net badge net
urltools::suffix_extract('www.bankofcyprus.com')
## host subdomain domain suffix
## 1 www.bankofcyprus.com www bankofcyprus com

Remove protocol, domainame, domain and file extension from URL

Everybody stand back! I know regular expressions!
Try this one:

var my_location = window.location.toString().match(/\/\/[^\/]+\/([^\.]+)/)[1];

Источник

PHP Remove Domain Name Extension from String

Solution 4: You can combine parse_url() and str_* functions, but you’ll never have correct result if you need cut domain zone (.com, .net, etc.) from your result. For example: » http://mydomain.com/ » into » mydomain » or » http://mydomain.co.uk/ » into » mydomain » I’m looking for a quick function that will allow me to remove such things as: «http://», «www.», «.com», «.co.uk», «.net», «.org», «/» etc Thanks in advance 🙂 Solution 1: To get the host part of a URL use : And for the rest see my answer to Remove domain extension .

PHP Remove Domain Name Extension from String

I was wondering of the best way of removing certain things from a domain using PHP.

For example:

» http://mydomain.com/ » into » mydomain «

» http://mydomain.co.uk/ » into » mydomain «

I’m looking for a quick function that will allow me to remove such things as:

«http://», «www.», «.com», «.co.uk», «.net», «.org», «/» etc

To get the host part of a URL use parse_url :

$host = parse_url($url, PHP_URL_HOST); 

And for the rest see my answer to Remove domain extension .

Could you use string replace?

This would strip out ‘http://’ from a string. All you would have to do first is get the current url of the page and pass it through any string replace you wanted to..

I would str_replace out the ‘http://’ and then explode the periods in the full domain name.

You can combine parse_url() and str_* functions, but you’ll never have correct result if you need cut domain zone (.com, .net, etc.) from your result.

parse_url('http://mydomain.co.uk/', PHP_URL_HOST); // will return 'mydomain.co.uk' 

You need use library that uses Public Suffix List for handle such situations. I recomend tldextract.

$extract = new LayerShifter\TLDExtract\Extract(); $result = $extract->parse('mydomain.co.uk'); $result->getHostname(); // will return 'mydomain' $result->getSuffix(); // will return 'co.uk' $result->getFullHost(); // will return 'mydomain.co.uk' $result->getRegistrableDomain(); // will return 'mydomain.co.uk' 

Php — Get current domain, @TonyEvyght that’s the point infgeoax and I try to make, you should get the host name you’re connecting with in $_SERVER[‘HTTP_HOST’].If the sites one.com and two.com are «redirecting» using an (i)frame, the page itself still comes from myserver.uk.com, so you won’t get the real domain. What is the HTML …

Читайте также:  Python websocket client reconnect

Php regex get first URL from string

I’m using the following regex in PHP to grab the URLs from a string

regex = '/https?\:\/\/[^\" ]+/i'; $string = "lorem ipsum http://google.com lorem ipusm dolor http://yahoo.com/something"; preg_match_all($regex, $string, $matches); $urls = ($matches[0]); 

$urls returns all the URLs. How can return only the first URL? In this case http://google.com .

I’m trying to achieve this without using a foreach loop.

According to the documentation:

Since you are after just one, you should be using preg_match :

$regex = '/https?\:\/\/[^\" ]+/i'; $string = "lorem ipsum http://google.com lorem ipusm dolor http://yahoo.com/something"; preg_match($regex, $string, $matches); echo $matches[0]; 

Use preg_match instead of preg_match_all

preg_match_all() has a flag parameter which you can use to order the results. The parameter in which you have the variable $matches is your results and should be listed in that array.

$matches[0][0];//is your first item. $matches[0][1];//is your second 

It would be better to use preg_match() rather than preg_match_all() .

Here’s the documentation on preg_match_all() for your flags. Link here!

Try this.Grab the capture or group.See demo.

$re = "/^.*?(https?\\:\\/\\/[^\\\" ]+)/"; $str = "lorem ipsum http://google.com lorem ipusm dolor http://yahoo.com/something"; preg_match_all($re, $str, $matches); 

Php — Get domain name from full URL, You need package that using Public Suffix List.Yes, you can use string functions arround parse_url() or regex, but they will produce incorrect result in complex URLs.

PHP — Get string from URL

by having an URL like this: mysite.com/subfolder/helloworld — Is it possible to read the «helloworld» from within a PHP page?

I would like to use the string as a part to load some content.

end( explode( '/', $_SERVER['REQUEST_URI'] ) ) 

without the end() call it will give you all the parts of the URL

You should read about URL rewriting.

This is more clean than «reading» the current URL. Basically you can redirect your URL (transparently) to something like :

mysite.com/index.php?folder=subfolder&category=helloworld 

Then in PHP, you can access the URL parameter with :

$folder = $_GET['subfolder'] $category = $_GET['category'] 

This is maybe not the kind of answer you were expecting, but it can be interesting to know.

The request url (everything from the first / after the domain name) can be found in $_SERVER[«REQUEST_URI»]

Hello have u used ‘strstr’ keyword of php? please try like this

if(strstr($_SERVER["REQUEST_URI"], "helloworld")) return true; else return false; 

May be this will be helpful to you.

Http — PHP — Get string from URL, Find centralized, trusted content and collaborate around the technologies you use most. Learn more

Источник

Оцените статью