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"
Related
I'm new to php. I really need your help to write a function to convert below URL
From orginal URL: http://www.domain.com/blahblah/**ID**/**FileName**.html
To new URL http://statics.domain.com/download/**ID**/**Filename**.mp4
I want to get ID and Filename in the new URL. Can anyone help me to do with this?
A dirty way which work :
$url = "http://www.domain.com/blahblah/ID/FileName.html";
$replace = array("http://www.domain.com/blahblah/", ".html");
$by = array("http://statics.domain.com/download/", ".mp4");
$newurl = str_replace($replace, $by, $url);
It's basicaly replace what you want by...what you want. But nothing more, and I'm pretty sure a better answer is possible technicaly-writing. ;)
A hack way is to explode() the string by / and take last two element of array. Second last element would be an ID where as last element would be a filename.
You will need to perform substr() on filename to remove last .html characters from string.
This is how you do it.
<?php
$url="http://www.domain.com/blahblah/ID/FileName.html";
$parts=explode("/",$url);
$totalparts=sizeof($parts);
$id=$parts[$totalparts-2];
$filename=substr($parts[$totalparts-1], 0, -5);
Demo: https://eval.in/659950
Solved. This is simple and works.
function converturl($url){
if(preg_match('/.*domain.com\/.*\/(.*?)\/(.*).html/is', $url, $id)){
$newurl = 'http://static.domain.com/download/'.$id[1].'/'.$id[2].'.mp4';
}else{
$newurl = 'URL is not support';
}
return $newurl;
}
Hi i want to know how can i get substring from string after last slash?
In short i want to get the file name from path.
for example i got string like this:
test-e2e4/test-e2e4/test-e2e4/6.png
and i want to get 6.png how can i do that ?
I got dir only and the file name can be all format, it can be also something else then file
Or test-e2e4/test-e2e4/test-e2e4/aaaaa and want to get aaaaa
Regex maybe? Or maybe you know some nice functions which will do it for me ?
In addition to other replies, there's actually a function in PHP to do this: basename. Example:
$string = 'test-e2e4/test-e2e4/test-e2e4/6.png';
$base = basename($string); // $base == '6.png';
$string = 'test-e2e4/test-e2e4/test-e2e4/aaaaa';
$base = basename($string); // $base == 'aaaaa'
$string = '6.png';
$base = basename($string); // $base == '6.png'
Full details here: http://php.net/basename
Do like this..
$yourstring = 'test-e2e4/test-e2e4/test-e2e4/6.png';
$val = array_pop(explode('/',$yourstring)); // 6.png
You can try explode and array_pop functions to work this out:
$str = 'test-e2e4/test-e2e4/test-e2e4/aaaaa.png';
$str = explode('/', $str);
$filename = array_pop($str);
echo $filename; //Output will be aaaaa.png
...Or you can use the following regex:
[^\/]*$
You don't need to use regex for this, you can use substr() to get a portion of the string, and strrpos() to specify which portion:
$full_path = "test-e2e4/test-e2e4/test-e2e4/6.png"
$file = substr( $full_path, strrpos( $full_path, "/" ) + 1 );
substr() returns a portion of the string, strrpos() tells it to start from the position of the last slash in the string, and the +1 excludes the slash from the return value.
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));
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
i want to give regex a pattern and force it to read it all ..
http://example.com/w/2/1/x/some-12345_x.png
i want to target "some-12345_x"
i used this /\/(.*).png/, it doesnt work for some reason
how do i force it to remember it must start with / and end with .png?
If you always want to get the final file-name, minus the extension, you could use PHP's substr() instead of trying to come up with a regex:
$lastSlash = strrpos($url, '/') + 1;
$name = substr($url, $lastSlash, strrpos($url, '.') - $lastSlash);
Also, a more readable method would be to use PHP's basename():
$filename = basename($url);
$name = substr($filename, 0, strpos($filename, '.'));
To actually use a regex, you could use the following pattern:
.*/([^.]+).png$
To use this with PHP's preg_match():
preg_match('|.*/([^.]+).png$|', $url, $matches);
$name = $matches[1];
You can do:
^.*/(.*)\.png$
which captures what occurres after the last / till .png at the end.
You might need to use reg-ex in this situation for a particular reason, but here's an alternative where you don't:
$url = "http://example.com/w/2/1/x/some-12345_x.png";
$value = pathinfo($url);
echo $value['filename'];
output:
some-12345_x
pathinfo() from the manual
How about:
~([^/]+)\.png$~
this will match anything but / until .png at the end of the string.