Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:

PHP - regular expression to remove domain name from row

Hey everyone,

I've been working on a PHP project where I have a dataset with rows of URLs. I need to remove the domain name from each row and keep only the path part.

For example, if I have a URL like "https://www.example.com/path/to/something", I want to remove "https://www.example.com" and keep "/path/to/something".

I believe regular expressions can help achieve this, but I'm not very experienced with them. Can someone please provide me with a regular expression in PHP that can help me achieve this?

Thank you in advance for your help!

All Replies



I had a similar situation where I needed to extract the path from URLs in a PHP project. I found that using regular expressions was an effective solution for this task.

To remove the domain name and retrieve the path, you can use the `parse_url` function in PHP. It parses a URL into its various components, including the path. Here's an example:

$url = "https://www.example.com/path/to/something";
$parsedUrl = parse_url($url);

if (isset($parsedUrl['path'])) {
$path = $parsedUrl['path'];
echo $path;

In this code snippet, `parse_url` extracts the different components of the URL, including the path. By accessing the 'path' key of the parsed URL, you can obtain the desired result.

When you run this code, you will get "/path/to/something" as the output. Additionally, `parse_url` takes care of handling various scenarios such as query parameters and fragments, so it provides a robust solution for URL manipulation.

I hope this alternative approach proves helpful to you! Let me know if you have any further queries.


Hey there!

I had a similar requirement in one of my PHP projects recently, and I can definitely help you out with this. To remove the domain name from a URL and keep only the path using regular expressions in PHP, you can use the `preg_replace` function.

Here's an example of how you can achieve this:

$url = "https://www.example.com/path/to/something";
$path = preg_replace('~^(?:https?://)?(?:www\.)?(.*?)/*$~', '', $url);

echo $path;

In this example, the regular expression pattern matches the optional `http://` or `https://` at the beginning, followed by an optional `www.`. The `(.*?)` captures the domain name, which is then replaced with an empty string using `preg_replace`. The `/*$` at the end accounts for any trailing slashes, if present.

By running this code, you would get the desired output as `/path/to/something`.

Feel free to adjust the regular expression pattern according to your specific requirements or any variations in the URLs you're handling.

I hope this helps! Let me know if you have any further questions.


Greetings everyone,

I encountered a similar requirement in a recent PHP project and tackled it using a slightly different approach. Instead of relying solely on regular expressions or the `parse_url` function, I opted for a combination of string manipulation and the `strpos` function.

Here's an example of how I accomplished it:

$url = "https://www.example.com/path/to/something";
$domain = "www.example.com";

$path = strstr($url, $domain);
$path = substr($path, strlen($domain));

echo $path;

In this code snippet, I first used `strstr` to retrieve the substring starting from the occurrence of the given domain name in the URL. Then, using `substr`, I removed the domain name portion from the extracted substring. Finally, I echoed the final result stored in the `$path` variable.

When you run this code, you should obtain "/path/to/something" as the output. This method allows for flexibility, particularly when the URLs follow a consistent format.

Please note that this method assumes that the input URLs have a definite structure with a consistent domain name. Adjustments may be needed if there are variations in the URL patterns.

If you have any further questions or need additional clarification, feel free to ask!

New to LearnPHP.org Community?

Join the community