So I see split is no good anymore or should be avoided.
Is there a way to remove the LAST Ampersand and the rest of the link.
Link Before:
http://www.websitehere.com/subdir?var=somevariable&someotherstuff&textiwanttoremove
Link After:
http://www.websitehere.com/subdir?var=somevariable&someotherstuff
Right now I am using this script:
<?php
$name = http_build_query($_GET);
// which you would then may want to strip away the first 'name='
$name = substr($name, strlen('name='));
//change link to a nice URL
$url = rawurldecode($name);
?>
<?php echo "$url"; ?>
It takes the whole URL (all Ampersands included)...the issue is, the site the link is coming from adds a return value &RETURNVALUEHERE, I need to remove the last "&" and the rest of the text after it.
Thanks!
Robb
using substr and strrpos
$url = substr($url, 0, strrpos($url, '&'));
you can use strrpos() like
$url = substr($orig_url, 0, strrpos($orig_url, "&"));
Without knowing the Real URL, I was able to come up with this:
<?php
// The string:
$string = "http://www.websitehere.com/subdir?var=somevariable&someotherstuff&textiwanttoremove";
// get the position of the last "&"
$lastPos = strrpos($string, "&");
// echo out the final string:
echo substr($string, 0, $lastPos);
If your input is already $_GET, removing the last value pair could simply be this:
http_build_query(array_slice($_GET, 0, -1));
Related
I have a string like this being entered into my database that I can't format before it gets stored :
image/upload/v1440427262/hglz466d8mm1pazysaoh.jpg#32e2e9a111a4f9f4aa01dbad2ca2aa403c994d28
The only part of that string that I want to use is this :
hglz466d8mm1pazysaoh.jpg
I'm trying to use strpos to remove the excess data.
So far I've managed to remove everything after and including the hashtag
($data is the original string) :
$dataclean = substr($data, 0, strpos($data, "#"));
This works as expected with $dataclean returning :
image/upload/v1440427262/hglz466d8mm1pazysaoh.jpg
But I don't know how to remove the rest of the excess data :
image/upload/v1440427262/
Also, can this all be done in one hit or does it have to be split into several operations?
Use basename:
$dataclean = basename(substr($data, 0, strpos($data, "#")));
If basename() doesn't work I would explode the string by the forward slash.
$data = 'image/upload/v1440427262/hglz466d8mm1pazysaoh.jpg#32e2e9a111a4f9f4aa01dbad2ca2aa403c994d28';
$pieces = explode("/", $data);
$dataclean = substr($pieces[3], 0, strpos($data, "#"));
As mentioned in the question, first remove values after # using
$link = substr($data, 0, strpos($data, "#"));
Then use basename() function to access filename from the URL.
For example,
$link = "http://example.com/folderPath/filename.php";
echo basename($link); // It will return filename.php
Nothing wrong with substr + strpos, but it looks like your string is a URL, so you could use parse_url to isolate the path before using basename. Just another option FYI.
basename(parse_url($yourString, PHP_URL_PATH));
You can use a regex function:
preg_match("/[a-z0-9]+\.[a-z]{3}/i", $input_line, $output_array);
Try the code
http://www.example.com/some_folder/some file [that] needs "to" be (encoded).zip
urlencode($myurl);
The problem is that urlencode will also encode the slashes which makes the URL unusable. How can i encode just the last filename ?
Try this:
$str = 'http://www.example.com/some_folder/some file [that] needs "to" be (encoded).zip';
$pos = strrpos($str, '/') + 1;
$result = substr($str, 0, $pos) . urlencode(substr($str, $pos));
You're looking for the last occurrence of the slash sign. The part before it is ok so just copy that. And urlencode the rest.
First of all, here's why you should be using rawurlencode instead of urlencode.
To answer your question, instead of searching for a needle in a haystack and risking not encoding other possible special characters in your URL, just encode the whole thing and then fix the slashes (and colon).
<?php
$myurl = 'http://www.example.com/some_folder/some file [that] needs "to" be (encoded).zip';
$myurl = rawurlencode($myurl);
$myurl = str_replace('%3A',':',str_replace('%2F','/',$myurl));
Results in this:
http://www.example.com/some_folder/some%20file%20%5Bthat%5D%20needs%20%22to%22%20be%20%28encoded%29.zip
Pull the filename off and escape it.
$temp = explode('/', $myurl);
$filename = array_pop($temp);
$newFileName = urlencode($filename);
$myNewUrl = implode('/', array_push($newFileName));
Similar to #Jeff Puckett's answer but as a function with arrays as replacements:
function urlencode_url($url) {
return str_replace(['%3A','%2F'], [':', '/'], rawurlencode($url));
}
I have this string:
$str="http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg";
Is there a built-in php function that can shorten it by removing the ._SL110_.jpg part, so that the result will be:
http://ecx.images-amazon.com/images/I/418lsVTc0aL
no, there's not any built in URL shortener php function, if you want to do something similar you can use the substring or create a function that generates a short link and stores the long and short value somewhere in database and display only the short one.
well, it depends if you need a regexp replace (if you don't know the complete value) or if you can do a simple str_replace like below:
$str = str_replace(".SL110.jpg", "", "http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg");
You can use preg_replace().
For example preg_replace("/\.[^\.]+\.jpg$/i", "", $str);
I would recommend using:
$tmp = explode("._", $str);
and then using $tmp[0] for your purpose, if you make sure the part you want to get rid of is always separated by "._" (dot-underscore) symbols.
You can try
$str = "http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg";
echo "<pre>";
A.
echo strrev(explode(".", strrev($str), 3)[2]) , PHP_EOL;
B.
echo pathinfo($str,PATHINFO_DIRNAME) . PATH_SEPARATOR . strstr(pathinfo($str,PATHINFO_FILENAME),".",true), PHP_EOL;
C.
echo preg_replace(sprintf("/.[^.]+\.%s$/i", pathinfo($str, PATHINFO_EXTENSION)), null, $str), PHP_EOL;
Output
http://ecx.images-amazon.com/images/I/418lsVTc0aL
See Demo
you could do this substr($data,0,strpos($data,"._")), if what you want is to strip everything after "._"
No, it is not (at least not directly). Such URL shorteners usually generate unique ID and remember your original URL and generated ID. When you enter such url, you start a script, which looks for given ID and then redirect to target URL.
If you want just cut of some portion of your string, then assuming that filename format is as you shown, just look for 1st dot and substr() to that place. Or
$tmp = explode('.', $filename);
$shortName = $tmp[0];
If suffix ._SL110_.jpg is always there, then simply str_replace('._SL110_.jpg', '', $filename) could work.
EDIT
Above was example for filename only. Whole code would be:
$url = "http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg";
$urlTmp = explode('/', $url);
$fileNameTmp = explode( '.', $urlTmp[ count($urlTmp)-1 ] );
$urlTmp[ count($urlTmp)-1 ] = $fileNameTmp[0];
$newUrl = implode('/', $urlTmp );
printf("Old: %s\nNew: %s\n", $url, $newUrl);
gives:
Old: http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg
New: http://ecx.images-amazon.com/images/I/418lsVTc0aL
So I'm trying to get the id from a url for youtube..
here is the url
http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2
then there's also - in the url too.
it wouldn't let me post another url but it's the same as above but with the id ucvkO0x-mL4
how can I grab between videos/ and /related (the id) with regex?
I tried to use txt2re.com which is what I always use, but it's not working for this case..
thanks!
No need for even regex, just a simple strpos and substr will do it. Or just use explode like this:
<?php
$url = 'http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2';
//BY STRPOS/SUBSTR
echo substr($url, 42, strpos($url, '/related', 42) - 42);
//BY EXPLODE
$parts = explode('/', $url);
echo $parts[6];
?>
(?:.*)videos/(.*?)/related\?v=2
This is how I would do it
$url = 'http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2';
preg_match('#.*videos/([0-9a-zA-Z_\-]{11})/related.*#', $url, $matches);
print_r($matches);
But, #shamittomar is right about strpos and substr
using regex you can do it by
$url = "http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2";
preg_match('/videos\/(.+)\/related/',$url,$match);
$id = $match[1];
How do you strip/remove wording in PHP?
I have a form that's passing a full URL link to an output page.
Example:
maps/africa.pdf
And on the output page, I want to provide an "href link", but in PHP use that same posted URL, but strip off the "maps" and have it provide a link that just says africa.
Example:
africa
can this be done?
Thanks!
Use pathinfo:
$filename = 'maps/africa.pdf';
$title = pathinfo($filename, PATHINFO_FILENAME);
If you want only .pdf to be stripped, use basename:
$filename = 'maps/africa.pdf';
$title = basename($filename, '.pdf');
$string = 'maps/africa.pdf';
$link_title = str_replace(array('maps/', '.pdf'), '', $string);
So you just want the file name? If so, then that would be everything between the last slash and the last dot.
if (preg_match("#/([^/]+)\\.[^\\./]+$#", $href, $matches)) {
$linkText = $matches[1];
}
Some good answers here. Also, if you know the url every time you could count the characters and use substr() e.g. http://uk3.php.net/substr
$rest = substr("abcdef", 2, -1); // returns "cde"