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);
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
i would like to replace the entire URL to completely new one if the URL currently contains an unwanted string.
this is what i tried.
if (preg_match('/(site1.com)/i')){
$url = preg_replace('/(site1.com)/i',('http://www.site2.com/newsite/index.php?a=b'),$url);
}
any help is appreciated.
It doesn't seem necessary to do a preg_replace if you want to replace the entire string. So, try this:
$url = 'http://www.site1.com/';
$pattern = '/(site1.com)/i';
$newUrl = 'http://www.site2.com/newsite/index.php?a=b';
if (preg_match($pattern, $url)){
$url = $newUrl;
}
print_r($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]."/";
?>
Trying to retrieve data from the database to put into a YouTube frame
When a person submits their link to their video, they send it as http://ww... I then need to convert that when it displays in the iframe as //ww... so how do I remove the http: from their links using preg_replace?
You can use ltrim
$newUrl = ltrim($url, 'http:');
You want regex like this:
$new = preg_replace( '/^https?:\/\//', '', $url );
That will ensure http:// and https:// are removed.
^ = start of the string
? = previous character optional
If your site is ONLY allowing http:// then #Aurelio is correct
Here you go.. preg_replace( "#^[^:.]*[:]+#i", "", $URL );
you can try this code below:
<?php
$url = "http://youtube.com";
$url = preg_replace( "#^[^:.]*[:]+#i", "", $url);
echo $url;
?>
You can achieve the same goal with str_replace() that is faster:
$newUrl = str_replace('http:, '', $url);
Using str_replace I want to change $url from this:
$url = http://example.com/images/lala1.jpg
to this
$url = http://example.com/images/lala1-0001.jpg
My problem is that I don't know how to insert the "-".
$url is changing so I really only know that it has ".jpg" at its' end.
My code so far:
for($i=1;$i<=9;$i++) {
$array[] = str_replace('.jpg',sprintf("%04d",$i).'.jpg',$url); }
Any idea how I can make this work?
You might want to make use of a regular expression:
$str = preg_replace('/\.jpg$/', sprintf("-%04d.jpg",$i), $url);
This insures that it ONLY gets replaced at the end of the string.
Sprintf allows for regular characters in the arguments:
str_replace('.jpg', sprintf("-%04d.jpg", $i), $url);