This question already has answers here:
PHP Regex to Remove http:// from string
(8 answers)
Closed 9 years ago.
I have a PHP script that removes the "http://" from user input url strings.
My Script:
$url= "http://techcrunch.com/startups/";
$url = str_replace('http://', '', $url);
Result:
$url= techcrunch.com/startups/
This works great, except that sometimes urls have "https://" instead. Is there a way I can just remove everything before the domain name, no matter what it is?
Try this out:
$url = 'http://techcrunch.com/startups/';
$url = str_replace(array('http://', 'https://'), '', $url);
EDIT:
Or, a simple way to always remove the protocol:
$url = 'https://www.google.com/';
$url = preg_replace('#^.+?\:\/\/#', '', $url);
Something like this ought to do:
$url = preg_replace("|^.+?://|", "", $url);
Removes everything up to and including the ://
Use look behinds in preg_replace to remove anything before //.
preg_replace('(^[a-z]+:\/\/)', '', $url);
This will only replace if found in the beginning of the string, and will ignore if found later
preg_replace('/^[^:\/?]+:\/\//','',$url);
some results:
input: http://php.net/preg_replace
output: php.net/preg_replace
input: https://www.php.net/preg_replace
output: www.php.net/preg_replace
input: ftp://www.php.net/preg_replace
output: www.php.net/preg_replace
input: https://php.net/preg_replace?url=http://whatever.com
output: php.net/preg_replace?url=http://whatever.com
input: php.net/preg_replace?url=http://whatever.com
output: php.net/preg_replace?url=http://whatever.com
input: php.net?site=http://whatever.com
output: php.net?site=http://whatever.com
$new_website = substr($str, ($pos = strrpos($str, '//')) !== false ? $pos + 2 : 0);
This would remove everything before the '//'.
EDIT
This one is tested. Using strrpos() instead or strpos().
Related
Need to remove random aa/ or bb/ to zz/ letters (with slash) to get /logo/picture.png
$url = "/logo/aa/picture.png";
$url = "/logo/bb/picture.png";
$url = "/logo/cc/picture.png";
This is an alternative which doesn't care what's contained in that url part or what lengths url parts have:
$urlParts = explode('/', $url);
array_splice($urlParts, count($urlParts) - 2, 1);
$url = implode('/', $urlParts);
If the $url is always of the form you provided you could do:
$str1 = substr($url,0,5);
$str2 = substr($url,8,strlen($url));
$url = $str1.$str2;
if it's not always of the same form you could determine the substrings indexes programmatically, maybe using strpos function. More detail here
Luca Angioloni solution is correct but this is more stable:
$url preg_replace("/\/[a-z]{2}\//", "/", $url);
This will work even on url like: /img/xz/picture.png but if you have an url like this /ig/aa/picture.png this will remove /ig and not /aa
This question already has answers here:
post processing on google urls
(2 answers)
Closed 8 years ago.
How can this string '/url?q=' that sits before each Google url be removed? I have tried regular expression, but it is not working.
<?php
$url = "/url?q=http://www.testinstrumentsafrica.com/&sa=U&ei=dTTTU7L2A4egugSY44LgAQ&ved=0CBMQFjAAOGQ&usg=AFQjCNGIavZUP46nbvLPJUrDXTgC3QF6aQ";
echo preg_replace("%/url?q=%", " ", $url);
?>
You don't really need regular expresions for fixed strings:
$url = "/url?q=http://www.testinstrumentsafrica.com/&sa=U&ei=dTTTU7L2A4egugSY44LgAQ&ved=0CBMQFjAAOGQ&usg=AFQjCNGIavZUP46nbvLPJUrDXTgC3QF6aQ";
$url = mb_substr($url, 7);
var_dump($url);
... or plain substr() if you aren't using UTF-8.
Try this:
echo preg_replace("%/url\\?q=%", " ", $url);
You need to escape ? or otherwise it represents an optional l
echo preg_replace("~\/url\?q=~", " ", $url);
The above code would replace the string /url?q= with a space.
DEMO
you could try this:
$parts = explode('/url?q=', $url);
$justUrl = $parts[0];
I'm trying to do some string matching in PHP. I have the following url string in a variable:
phones/gift.nintendo-3ds/handset.blackberry-9790.html
I want remove the /gift.nintendo-3ds from the above, but the gift will always be different.
Any ideas? I want the url variable to look like this after each call different gifts:
phones/handset.blackberry-9790.html
Thanks
preg_replace('/\/gift\.[^/]*/', '', $url);
Matches /gift. then anything till the next slash and replaces it with blank.
Try with:
$input = 'phones/gift.nintendo-3ds/handset.blackberry-9790.html';
$output = preg_replace('(gift\.[^/]*\/)', '', $input);
You could split it apart, remove the second part you do not want to keep and then concat it again:
$parts = explode('/', $url, 3);
unset($parts[1]);
$result = implode('/', $parts);
This is not using any regular expression as you might have thought about but probably tells you about some other useful functions.
Demo: http://codepad.org/a1pNW8J6
A regex variant could be:
echo preg_replace('~^([^/]+)(/[^/]+)~', '$1', $url);
Demo: http://codepad.org/vyR04xMn
eg:
$url=http://www.example.com/.
how to make the $url to this style.
http://test.google.com/example.com in php?
PHP: Simple and easy way to format URL string should clear everything up for you
$url_parts=parse_url($url);
echo $url="http://test.google.com/".str_replace('www.','',$url_parts['host']);
$url = "http://www.example.com";
$Step1 = str_replace(array("http://", "https://", "www."), "", $url);
$Step2 = explode("/", $Step1);
$newUrl = "http://test.google.com/".$Step2[0];
Basically what I did is replacing any http://, https:// and www. strings from the URL in $url and replace them with a blank string. Then I explode the result of this replace on an '/' character, because there might be an URL given as http://www.test.com/mydir/ so we lose the mydir. If this isn't want you need, skip step 2 and replace $Step2[0] with $Step1 on the last line.
This last line adds the URL you want in $newUrl
Try this:
$url = "http://www.example.com/";
$url = preg_replace("/(?:http:\/\/)?(?:www\.)?([a-z\d-\.]+)\/.*/", "http://test.google.com/$1", $url);
The code below removes "www.", etc. from the beginning of websites that are entered into a database. It works great.
Is there a way I could use similar code to remove a forward-slash from the tail-end of a website that is entered into the same database?
$remove_array = array('http://www.', 'http://', 'https://', 'https://www.', 'www.');
$site = str_replace($remove_array, "", $_POST['site']);
You can pass a string of characters that you want trimmed off of a string to the trim family of functions. Also, you could use rtrim to trim just the end of the string:
$site = rtrim($site, "/");
$site = preg_replace('{/$}', '', $site);
This uses a relatively simple regular expression. The $ means only match slashes at the end of the string, so it won't remove the first slash in stackoverflow.com/questions/. The curly braces {} are just delimiters; PHP requires matching characters and the front and back of regular expressions, for some silly reason.
Simplest method:
$url = rtrim($url,'/');
John was the first and I think his solution should be preferred, because it's way more elegant, however here is another one:
$site = implode("/", array_filter(explode("/", $site)));
Update
Thx. I updated it and now even handles things like this
$site = "///test///test//"; /* to => test/test */
Which probably makes it even cooler than the accepted answer ;)
Is that what You want?
$url = 'http://www.example.com/';
if (substr($url, -1) == '/')
$url = substr($url, 0, -1);
You could have a slash before the question mark sign and you could have the "?" sign or "/" in the parameters (.com/?price=1), so you shouldn't delete this every time. You need to delete only the first slash "/" before the question mark "?" or delete the last slash "/" if you have no question marks "?" at all.
For example:
https://money.yandex.ru/to/410011131033942/?&question=where?&word=why?&back_url=https://money.yandex.ru/to/410011131033942/&price=1
would be
https://money.yandex.ru/to/410011131033942?&question=where?&word=why?&back_url=https://money.yandex.ru/to/410011131033942/&price=1
And
https://money.yandex.ru/to/410011131033942/
would be
https://money.yandex.ru/to/410011131033942
The PHP code for this would be:
if(stripos($url, '?')) {
$url = preg_replace('{/\?}', '?', $url);
} else {
$url = preg_replace('{/$}', '', $url);
}
The most elegant solution is to use rtrim().
$url = 'http://www.domain.com/';
$urlWithoutTrailingSlash = rtrim($url, '/');
EDIT
I forgot about rtrim();
You could also play around parse_url().
$new_string = preg_replace('|/$|', '', $string);
Perhaps a better solution would be to use .htaccess, but php can also do it with something like this:
<?php
header('location: '.preg_replace("/\/$/","",$_SERVER['REQUEST_URI']));
?>