How to Remove the Url Protocol and Slash from User Input in PHP

How do I remove the URL protocol and slash from user input in PHP

You should use an array of "disallowed" terms and use strpos and str_replace to dynamically remove them from the passed-in URL:

function remove_http($url) {
$disallowed = array('http://', 'https://');
foreach($disallowed as $d) {
if(strpos($url, $d) === 0) {
return str_replace($d, '', $url);
}
}
return $url;
}

How do I remove http, https ,slash,www from user input in php

Might be a better one but this should do it:

$result = preg_replace('#^(https?://|ftps?://)?(www.)?#', '', $str);
  • (http with optional ? s :// OR | ftp with optional ? s ://) with both optional ?
  • www. optional ?

How to remove http://, www and slash from URL in PHP?

$input = 'www.google.co.uk/';

// in case scheme relative URI is passed, e.g., //www.google.com/
$input = trim($input, '/');

// If scheme not included, prepend it
if (!preg_match('#^http(s)?://#', $input)) {
$input = 'http://' . $input;
}

$urlParts = parse_url($input);

// remove www
$domain = preg_replace('/^www\./', '', $urlParts['host']);

echo $domain;

// output: google.co.uk

Works correctly with all your example inputs.

PHP Regex to Remove http:// from string

$str = 'http://www.google.com';
$str = preg_replace('#^https?://#', '', $str);
echo $str; // www.google.com

That will work for both http:// and https://

Remove protocol and result with full base_url from html input string

I hope I understand your question correctly. Are you having trouble with the regex or the code for looping over the urls? Or both?

I'm going to assume both...

Instead of matching the whole thing and grouping the bit you want to extract, I'd suggest you match just what you want to extract. With that in mind, the regex could look like this:

[^/]+\.[^/:]{2,3}

In english this says:

Match anything except a forward slash until there is a dot, then match between 2 and 3 more of anything except a forward slash or a colon

This seems simple, but i think it gets you what you need.

Here is a bit of php code that creates an array of urls in various formats and then loops though each one and extracts just the bit i think you want. I've switched to using preg_match instead of preg_replace because i think it makes more sense in this case:

<?php
$urls = array(
"https://lab1.sfo1.transparentpixel.co.jp:1935/rtsp/_definst_",
"http://lab1.sfo1.transparentpixel.com:1935/rtmp/_definst_",
"http://lab1.sfo1.transparentpixel.com/rtmp/_definst_",
"lab1.sfo1.transparentpixel.com",
"someurl.com:1935/rtmp/_definst_",
"someurl.com/_definst_",
"http://someurl.co.uk");

foreach($urls as $url)
{
preg_match('%[^/]+\.[^/:]{2,3}%m', $url, $matches);
echo $matches[0]; // instead of this you could do $test[] = $matches[0];
}
?>

You'll notice that I'm looping over the array using a foreach loop which means we are not limited to a fixed number of iterations as in your example.

The output of this is:

lab1.sfo1.transparentpixel.co.jp
lab1.sfo1.transparentpixel.com
lab1.sfo1.transparentpixel.com
lab1.sfo1.transparentpixel.com
someurl.com
someurl.com
someurl.co.uk

Issue with PHP trim() - removing https:// from the string, a strange issue

The trim function removes any of the supplied characters, not literal matches. Since .net contains a t is removing it.

You should use str_replace() instead. For more advanced manipulations you could consider parse_url()



Related Topics



Leave a reply



Submit