Remove characters from end of URL - php

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]."/";
?>

Related

Remove only 3 characters after a specific string

Hi I have this string for example
http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/it/clients/
I want to remove only 3 characters after bbbb-bbbbbbbbbb-2/, so basically I want to remove the it/ part (This it/ part may not always be it but it can be es/ or en/ or different languages always 2 characters )
The following will work provided the the URL structure doesn't change. I assume you're wanting to remove the language part of the URL.
<?php
$url = "http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/it/clients/";
$parsedURL = parse_url($url);
$path = explode('/', $parsedURL['path']);
unset($path[2]);
$url = "{$parsedURL['scheme']}://{$parsedURL['host']}";
$url .= implode('/', $path);
var_dump($url);
// string(47) "http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/clients/"
You can use regex to selecting target part of string and run it in preg_replace().
$url = "http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/it/clients/";
echo preg_replace("#(.*)\w{2}/([^/]+/)$#", "$1$2", $url);
See result of code in demo
Define your languages in an array. If a language is not defined in the array URL will be the same.
$mLanguages = ["en","it","bg","gr"];
$mURL = "http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/it/clients/";
$mURL = removeLanguageFromURL($mURL, $mLanguages);
echo $mURL; // Output http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/clients/
function removeLanguageFromUrl($mURL, $mLanguages){
foreach($mLanguages as $language){ // Search languages
if(strpos($mURL, $language) !== false) // If language is found in url remove it
$mURL = str_replace('/' . $language,'', $mURL);
}
return $mURL;
}

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

How to remove last part of url in PHP

$url = explode('/', $articleimage);
$articleurl = array_pop($url);
I have used the above method to get the last part of a URL.Its working.But I want to remove the last part from the URL and display the remaining part.Please help me.Here I am mentioning the example URL.
http://www.brightknowledge.org/knowledge-bank/media/studying-media/student-media/image_rhcol_thin
Try this:
$url = explode('/', 'http://www.brightknowledge.org/knowledge-bank/media/studying-media/student-media/image_rhcol_thin');
array_pop($url);
echo implode('/', $url);
There is no need to use explode, implode, and array_pop.
Just use dirname($path). It's a lot more efficient and cleaner code.
Use the following string manipulation from PHP
$url_without_last_part = substr($articleimage, 0, strrpos($articleimage, "/"));
For Laravel
dirname(url()->current())
In url()->current() -> you will get current URL.
In dirname -> You will get parent directory.
In Core PHP:
dirname($currentURL)
after the array_pop you can do
$url2=implode("/",$url)
to get the url in a string
Change this:
$articleurl = array_pop($url);
Into this:
$articleurl = end($url);
$articleurl will then hold the last array key.
Missed the part where you want to remove the value, you can use the function key() to get the key and then remove the value using that key
$array_key = key($articleurl);
unset(url[$array_key])
Pretty simple solution add in the end of your code
$url = implode('/', $url);
echo $url;
Notice that array_pop use reference argument passing so array will be modifed implode() function does the opposite to explode function and connects array elements by first argument(glue) and returns the string.
It looks like this may be what you are looking for. Instead of exploding and imploding, you can use the parsing functions which are designed to handle exactly this kind of URL manipulation.
$url = parse_url( $url_string );
$result =
$url['scheme']
. "://"
. $url['host']
. pathinfo($url['path'], PATHINFO_DIRNAME );
Here's the simple way to achieve
str_replace(basename($articleimage), '', $articleimage);
For the one-liners:
$url = implode('/', array_splice( explode('/', $articleimage), 0, -1 ) );
$url[''] and enter the appropriate number

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

Remove URL regardless of format

Having a brain freeze...
Have a URL which may be in any of the formats :
http://url.com/stuff
url.com/somestuff
www.url.com/otherstuff
https://www.url.com/morestuff
You get the picture.
How do I remove the .com part to leave just the various 'stuff' parts ? For example, the above would end up :
stuff
somestuff
otherstuff
morestuff
You could achieve that using the following code:
$com_pos = strpos($url, '.com/');
$stuff_part = substr($url, $com_pos + 5);
Click here to see the working code.
This should do the trick for you!
<?php
$url = "http://url.com/stuff";
$querystring = preg_replace('#^(https|http)?(://)?(www.)?([a-zA-Z0-9-]+)\.[a-zA-Z]{2,6}/#', "", $url);
echo $querystring;
I submitted this answer because I'm not very fond of solutions using explode() to handle this. Maybe your query string contains more slashes so, you'd have to write exceptions for those cases.
You can use explode to make an array, then get the last element from the array.
$str = 'http://url.com/stuff';
$arr = explode('/', $str);
echo end($arr); // 'stuff'
$path = parse_url('http://url.com/stuff', PHP_URL_PATH);
If you leave the second parameter unspecified you can return an array including the domain etc.
Use explode function to divide the string.
<?php
$url = "http://url.com/stuff";
$stuff = explode("/", $url);
echo $stuff[sizeof($stuff) - 1];
?>
I used sizeof to access to last element.
preg_replace("/^(https?:\/\/)?[^\/]+/" ,"", $url);

Categories