sorry because I don't know how to ask the question
I have here a code in php
$productDetails['country'][0]['image']
which output is:
http://localhost/wine-works.co.uk-tester/wp-content/uploads/2013/09/canada-wine_shop.jpg
how do I change the filename to look like this:
http://localhost/wine-works.co.uk-tester/wp-content/uploads/2013/09/canada-wine_shop-25x17.jpg
$d = pathinfo($productDetails['country'][0]['image']);
echo $new_name = $d['dirname'].'/'.$d['filename'].'-25x17.'.$d['extension'];
$str= 'http://localhost/wine-works.co.uk-tester/wp-content/uploads/2013/09/canada-wine_shop.jpg';
$suffix = '-25x17';
$name = substr($str,strrpos($str,'/')+1);
$leftString = substr($str,0,strrpos($str,'/')+1);
$extension = substr($name,strrpos($name,'.'));
$nameNoExtension = substr($name,0,strrpos($name,'.'));
$string = $nameNoExtension.$suffix.$extension;
echo $leftString.$string;
//http://localhost/wine-works.co.uk-tester/wp-content/uploads/2013/09/canada-wine_shop-25x17.jpg
Related
How to replace url with php
why this code not work .
please help
$mylogo = 'http://example.com/images/logo.png';
$inputmylogo = 'http://example.com/image.png';
$file = 'setting.php';
$content = file_get_contents($file);
var_dump(preg_replace('/'.$mylogo.'/', $inputmylogo, $content));
You want to employ str_replace here:
$mylogo = 'http://example.com/images/logo.png';
$inputmylogo = 'http://example.com/image.png';
$file = 'setting.php';
$content = file_get_contents($file);
var_dump(str_replace('/'.$mylogo.'/', $inputmylogo, $content));
How can I name a txt file after a site URL without the preceding https:// or http:// as in: www.google.com.txt?
I think I might be getting it wrong here: fopen($sitenameWithoutHTTP.".txt, "w");
Below is the way I'm trying to address that:
<?php
//
#mkdir("result", 0755);
#chdir("result");
$link = $sitename;
$sitename = preg_replace('#^https?://#', '', $sitenameWithoutHTTP);
$resultfile = fopen($sitenameWithoutHTTP.".txt", "w");
//
?>
Thanks for helping find a fix.
Hope this helps you achieve what you intended!
<?php
$siteName = 'https://www.google.com';
$siteNameWithoutHttps = preg_replace('#^https?://#', '', $siteName);
// print_r($siteNameWithoutHttps);
$resultFile = fopen($siteNameWithoutHttps.".txt", "w");
// run a check
if($resultFile == true) {
echo "success";
} else {
echo "failed";
}
The expected result for the commented print_r above should be:
www.google.com
$arr = ['http','https',':','/','?','&','#','.'];
$sitename = str_replace($arr, '', $sitenameWithoutHTTP);
also you can use base64_encode(), or use parse_url().
$HOST = parse_url($sitenameWithoutHTTP, PHP_URL_HOST);
and if you need to save real URL and get it again I see best way with using Md5 hash
$fileName = md5($sitenameWithoutHTTP).'.txt';
can get it again file.php/?getFile2url=[httpLink]
header('Location: '. Md5($_GET['getFile2url']).'.txt');
exit;
This question already has answers here:
Get Last Part of URL PHP
(14 answers)
Closed 7 years ago.
I need to remove every characters before the last "/"
This my url :
http://www.example.com/highlights/cat/all-about-clothing/
And I want to have only :
all-about-clothing
Thanks
Use basename()
$str = 'http://www.example.com/highlights/cat/all-about-clothing/';
echo basename($str);
// Outputs: all-about-clothing
EDIT:
Another Solution:
$str = 'http://www.example.com/highlights/cat/all-about-clothing/';
$path = pathinfo($str, PATHINFO_BASENAME);
echo "<br/>" . $path;
Use PHP's parse_url() function.
edit:
basename() or pathinfo() is the easier way.
$str = 'http://www.example.com/highlights/cat/all-about-clothing/';
$str = trim($str,'/');
$str = explode('/',$str);
echo $str = end($str);
// get result
all-about-clothing
<?php
$url = "http://www.example.com/highlights/cat/all-about-clothing/";
$url_path = parse_url($url, PHP_URL_PATH);
$basename = pathinfo($url_path, PATHINFO_BASENAME);
echo $basename;
?>
You can use regex too:
$match = [];
$subject = 'http://www.example.com/highlights/cat/all-about-clothing/';
$pattern = '/http:\/\/www\.example\.com\/highlights\/cat\/(.*)/';
preg_match($pattern, $subject, $match);
print_r($match);
You can see the result here.
<?php
$url = 'http://www.example.com/highlights/cat/all-about-clothing/';
$basename = split('/',$url);
echo $basename[5];
?>
Most of the above solution focus on the exact example URL,
Please be careful as if extra params are added to the end of the string,
you may get the wrong result:
http://www.example.com/highlights/cat/all-about-clothing/?page=1
http://www.example.com/highlights/cat/all-about-clothing/item/1
to cache the 3rd "directory" after the domain name, and ignore the rest of the URL, the following code can be used:
$url = "http://www.example.com/highlights/cat/all-about-clothing/item/1";
$url_path = parse_url($url, PHP_URL_PATH); # /highlights/cat/all-about-clothing/item/1
$dirs = explode('/', $url_path); # Array([0] =>"", [1]=>"highlights", [2]=>"cat", [3]=>"all-about-clothing", [4]=>"item", [5]=>"1")
echo $dirs[3]; # all-about-clothing
I have a URL like below:
http://abc.def.com/gh-tqa/name-of-the-file/ZW607CCF.html
Now I want to get the string of ZW607CCF without .html.
How can I achieve this using PHP?
You don't need a regex here. Just use pathinfo():
$url = 'http://abc.def.com/gh-tqa/name-of-the-file/ZW607CCF.html';
$filename = pathinfo($url, PATHINFO_FILENAME);
There are lot of ways in PHP, try preg_replace(), for example:
$url = 'http://abc.def.com/gh-tqa/name-of-the-file/ZW607CCF.html';
$value = preg_replace("|.*/([^.]+)\.html|", "$1", $url);
this is how you do:
$temp = explode('.', $_SERVER['PHP_SELF']);
$string = $temp[0];
with PHP 5.4+ you can also do:
$string = explode('.', $_SERVER['PHP_SELF'])[0];
forexample, I have this url:
http://adstorage.jamba.net/storage/view/325/0/fa/Fairytale.mp3
How can I use PHP code so that it returns Fairytale. all the things before Fairytale and after that .mp3 should be removed.
the idetifier are / and .mp3 only the file name should be returned.
Use pathinfo() Function
<?php
$url = "http://example.com/file_name.mp3";
$f = pathinfo($url);
echo $f[filename]; // File name
?>
How about something like this:
$url = "http://adstorage.jamba.net/storage/view/325/0/fa/Fairytale.mp3";
$parts = parse_url($url);
$path_parts = explode('/', $parts['path']);
list($name, $extension) = explode(".", $path_parts[count($path_parts) - 1]);
echo $name;
This is probably overkill, and this thing could be done with a simple regex like:
preg_match("#.*/(\w+)\.\w+#", $url, $matches);
echo $matches[1];
Use pathinfo Function
<?php
$url = "http://adstorage.jamba.net/storage/view/325/0/fa/Fairytale.mp3";
$f = pathinfo($url);
echo $f[filename]; // File name Fairytale
print_r(pathinfo($url)); // File Array
?>
You can tryit with:
$input = 'http://adstorage.jamba.net/storage/view/325/0/fa/Fairytale.mp3';
echo current(explode('.', basename($input)));
Try in one line, $input being yout url (php 5.4) :
$name = explode('.',array_slice(explode('/',$input), -1, 1))[0];