My pages are www.example.com/somthing/types.html , www.example.com/somthing2/types.html this html files has a tag<a href="animals.html" . somthing,somthing2are files every file contains links in types.html and files like animals.html in the same folders.
when i get it with dom it shows only "animals.html" but i want to get "www.example.com/somthing/animals.html". how can i remove types.html from www.example.com/somthing/types.html and put www.example.com/somthing+/animals.html.
i just need a way to remove "types.html" from www.example.com/somthing/types.html.
keep the folder and remove the last part after this "/".
i dont know always last part (file name) so i need a way to remove last thing after last "/". sorry about my language problems.
str_replace('types.html' ,'','www.example.com/somthing/types.html');is for if i know the file name (types.html).
You can use this
preg_replace('/([^\/]*$)/', '', 'www.example.com/somthing/types.html');
This will replace all characters after the last /
Output will be www.example.com/somthing/
Try this
$string = 'www.examle.com/somthing/types.html';
echo preg_replace('#\/[^/]*$#', '', $string);
If you are considering the fast and the fastest you may consider this piece of code instead preg_replace
$url = explode("/",$url);
array_pop($url);
$url = implode("/",$url);
Again it is slighly faster so do as you please :).
Proof:
http://sandbox.onlinephpfunctions.com/code/5083e02d4f171502ef1e7c87c8dd9a957ee0d8fb
You can also do this the old fashion way:
$url = 'www.example.com/somthing/types.html';
$divided = explode( '/', $url );
array_pop( $divided );
$new_url = implode( '/', $divided );
Related
I've this code here:
$url = 'https://www.my-page.de/account/show/4913';
echo substr( $url, strrpos( $url, '/' ) + 1 );
This returns me the needed id:
4913
Now the problem begins. In some cases the URL looks like this:
$url = 'https://www.my-page.de/account/show/4913/';
$url = 'https://www.my-page.de/account/show/4913/?conversationId=xxx';
This means that my code don't works anymore. So is there any way to be 100 % sure that I always get my ID from the URL? The ID is always at the same position from the beginning on but the end can be different.
Update
When I've this code here I'm getting not the last part anymore. Any idea why and how to fix this?:
$url = 'https://my-page.de/account/show/4913/';
$id = basename( dirname( $url ) );
I just want to be sure that it works in every situation. In this case the selected part is:
show
You could use functions that are meant for directories and/or URLs:
echo basename(dirname($url));
//or
echo basename(pathinfo($url, PATHINFO_DIRNAME));
//or
echo basename(parse_url($url, PHP_URL_PATH));
The last one may return the filename if you had https://www.my-page.de/account/show/4913/index.php so you would want to use:
echo basename(dirname(parse_url($url, PHP_URL_PATH)));
Lot's of possibilities depending on what you need. The point is that there are specific functions for working with directories, filenames and URLs so that you don't have to treat them as just strings that have no meaning but unlimited possibilities..
Filesystem Functions
URL Functions
Just use explode() to turn it into an array, then get the 5th item to get the id:
<?php
$url = 'https://www.my-page.de/account/show/4913/?conversationId=xxx&trey=trey';
$arr = explode('/', $url);
$id = $arr[5];
echo '<pre>'. print_r($id, 1) .'</pre>';
then it doesn't matter how many query params there are, they'll always be last
refs:
https://www.php.net/manual/en/function.explode.php
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;
}
I am not much used to using rtrim and Reg expressions. So I wanted to get my doubt cleared about this:
Here is a url: http://imgur.com/r/pics/paoWS
I am trying to use rtrim function on this url to pick out only the 'paoWs' from the whole url.
Here is what i tried:
$yurl = 'http://imgur.com/r/pics/paoWS';
$video_id = parse_url($yurl, PHP_URL_PATH);
$yid=rtrim( $video_id, '/' );
And i am using '$yid' to hotlink the image from imgur. But What I get after trying this function is:
$yid= '/r/pics/paoWS'
How do I solve this?
rtrim is used for trimming down a string of certain characters or whitespace on the right-hand side. It certainly shouldn't be used for your purpose.
Assuming the URL structure will always be the same, you could just do something like this:
$yurl = 'http://imgur.com/r/pics/paoWS';
$video_id = parse_url($yurl, PHP_URL_PATH);
$parts = explode('/', $video_id)
$yid = end($parts);
You sould not use regular expressions (whitch are 'expensive') for a so 'simple' problem.
If you want to catch the last part of the URL, after the last slash, you can do :
$urlParts = explode('/', 'http://imgur.com/r/pics/paoWS');
$lastPart = end($urlParts);
rtim( strrchr('http://imgur.com/r/pics/paoWS' , '/') ); rtrim + strrchr
substr(strrchr('http://imgur.com/r/pics/paoWS', "/"), 1); substr + strrchr
rtrim() returns the filtered value, not the stripped characters. And your usage of it isn't proper too - it strips the passed characters from the right side. And you don't need parse_url() either.
Proper answers have been given already, but here's a faster alternative:
$yid = substr($yurl, strrpos($yurl, '/')+1);
Edit: And another one:
$yid = ltrim(strrchr($yurl, '/'), '/');
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
ok so im making a file system viewer.
Im stuck on this though, I have everything working fine except when the I click the back button on the gui I have made.
So I have a string like so
http://www.grubber.co.nz/update/index.php?dir=../developer/_social_development
The script will go to the folder /developer/_social_development
but when I want to go back I press the back button and it will go back to the top directory so it goes all the way back to the first '/' for example http://www.grubber.co.nz/update/index.php?dir=../
I use this code to get back to the last page which doesn't work
$dir = $_GET['dir'];
$marker = "/";
echo $str = (substr($dir, 0, (strpos($dir, $marker) + strlen($marker))));
all it does is remove everthing to the first '/' but I want it to goto the last '/' for example it was this /developer/_social_development and when I click on the previous folder i want it to be /developer also the string will change depending on what folder you are in so I cant just remove a set amount of characters
Thanks for the help
Using another method str pos, you may use explode. here complete code:
$dir = '../developer/_social_development';
$marker = "/";
$arrDir = explode('/', $dir);
array_pop($arrDir);
$dir2 = implode( '/', $arrDir);
echo $dir2;
result: ../developer
Instead of using strpos() which gives you the position of the first occurrence of a character in a string you should use strrchr() that will give you the position of the last occurrence of the character in your string
Not a pretty best solution, personally I'd use a RegEx approach, but if you wanted to use str position you could do something like:
$dir = $_GET['dir'];
$marker = "/";
echo $str = strrev(substr(strrev($dir), (strpos($dir, $marker) + strlen($marker))));
Or a RegEx based solution:
$dir = $_GET['dir'];
echo $str = preg_replace("/\/[^\/]+$/", "", $dir);
$path = '/foo/bar';
$path = $path . '/..'; // /foo/bar/..
echo realpath($path); // Shows /foo