get url after http:// from complete url string using php - php

i am storing url string in database like this,
'http://www.domain.com'
how can i display above string like
'www.domain.com'
I need regular expression in first hand

with parse_url() you can split a url into it's parts and get the ones you want to. in your case (looking for the host), it would be:
$url = 'http://www.domain.com';
$parts = parse_url($url);
$result = $parts['host'];
this has a big advantage over the other posted solutions: it's ready to go with https-urls, urls with get-parameters and/or url's including htaccess-authentication paramneters without having to change the code.

simply you can use
$url = 'http://www.domain.com';
str_replace('http://', '', $url);
it will return exactly www.domain.com

A general solution for modifying strings without parsing the URL (i.e. you should learn to be able to do something like this yourself):
if (stripos($url, 'http://', 0) == 0)
$url = substr($url, 7);
You also could easily add another check for a https:// prefix or whatever else:
if (stripos($url, 'https://', 0) == 0)
$url = substr($url, 8);
With a regular expression, albeit completely unnecessary as there is nothing that requires one, you could to it like this:
$url = preg_replace('/^http:\\/\\//', '', $url);
Or with direct support for https:
$url = preg_replace('/^https?:\\/\\//', '', $url);

You can use something like below:
<?php
str_replace('http://', '', $url);
Edit:
Since you insist on regex solution, here's one that takes care of http and https in one
echo preg_replace( '/^(htt|ht|tt)p\:?\/\//i', '', $url);

echo substr('http://www.domain.com',7);
if (substr('http://www.domain.com',7) == 'http://' or substr('https://www.domain.com',8) == 'https://')

Related

How to remove two same and random characters following each other in string - PHP

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

Remove characters from end of URL

Say I have a URL with something like this:
http://website.com/website/webpage/?message=newexpense
I have the following code to try and get the the URL before the question mark:
$post_url = $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$link_before_question_mark = explode('?', $actual_link);
$add_income_url = $link_before_question_mark[0];
In this example I would get the following URL:
http://website.com/website/webpage/
I'd like to remove the webpage portion of this so the URL is:
http://website.com/website/
How can I do this?
Use parse_url This way you have all components.
$url = 'http://website.com/website/webpage/?message=newexpense';
$pUrl = parse_url( $url);
echo $pUrl['scheme'] . '://' . $pUrl['host'] . $pUrl['path'];
You can do a similar trick using explode. Then pop the parts you don't need and implode the url back together. If you are sure that the part after '?' never contains a '/', you can replace your code with this one. If you're not sure, you should first remove the part after '/' and then run this code to remove the last part of the path.
<?php
$url = 'http://website.com/website/webpage/?message=newexpense';
$parts = explode('/', $url);
// Remove the last part from the array
$lastpart = array_pop($parts);
// If the last part is empty, or the last part starts with a '?'
// this means there was a '/' at the end of the url, so we
// need to pop another part.
if ($lastpart == '' or substr($lastpart, 0, 1) == '?')
array_pop($parts);
$url = implode('/', $parts);
var_dump($url);
I'd probably use dirname; it's specifically designed to strip the last stuff after a "/"...
$url = "http://website.com/website/webpage/?message=newexpense";
echo dirname(dirname($url))."/"; // "http://website.com/website/"
(As it says in the documentation, "dirname() operates naively on the input string, and is not aware of the actual filesystem...", so it's quite safe to use for this kind of purpose.)
Try it with explode
<?php
$actual_link = "http://website.com/website/webpage/?message=newexpense]";
$link_before_question_mark = explode('?', $actual_link);
$add_income_url = $link_before_question_mark[0];
$split=explode('/', $add_income_url);
echo $split[0]."//".$split[2]."/".$split[3]."/";
?>
Even better is...
<?php
$actual_link = "http://website.com/website/webpage/?message=newexpense]";
$split=explode('/', $actual_link);
echo $split[0]."//".$split[2]."/".$split[3]."/";
?>

how change the string in php?

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);

(Solved) need php script to remove the slash from the end of the url [duplicate]

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']));
?>

Replace a part of a url with php

Hey there!
I have a simple question that I am struggling with, hope you guys can have a look.
I have a input field where users would put in YouTube links and your typical single page works fine, for example:
youtube.com/watch?v=c3sBBRxDAqk
this watch?v=11characters works fine
but if the users inputs anything other than the above example, such as:
youtube.com/watch?v=tC0E1id4raw&feature=topvideos
//or
youtube.com/watch?v=smETLCCPTVo&feature=aso
is there a way to take the 2 above urls and remove any characters after the watch?v=11characters?
so in essence, turn this
$url = "youtube.com/watch?v=tC0E1id4raw&feature=topvideos"
into
youtube.com/watch?v=tC0E1id4raw removing & and onwards
I had to remove the http bit due to spam prevention
is there a simple way to do this?
$url = "youtube.com/watch?v=tC0E1id4raw&feature=topvideos";
list($keep, $chuck) = explode('&', $url);
echo $keep;
One way to do this is using explode:
$url = "youtube.com/watch?v=tC0E1id4raw&feature=topvideos";
$newurl = explode("&", $url);
Everything before the "&" will be in $newurl[0], and everything after it will be in $newurl[1].
No need for regex:
$parts = parse_url($url);
$params = array();
parse_str($parts['query'], $params);
If you have PECL pecl_http installed:
$url = http_build_url($url,
array('query' => 'v='. $params['v']),
HTTP_URL_REPLACE | HTTP_URL_STRIP_FRAGMENT);
Or without pecl_http:
$url = $parts['scheme'] . $parts['host'] . $parts['path'] . '?v=' . $params['v'];
This is more robust against changes of the order of the query parameters.
Reference: parse_url, parse_str, http_build_url

Categories