I have the following url - this url is not always the same though, but will always end the same:
$thumbnail_url = 'http://i2.ytimg.com/vi/552yWya5RgY/hqdefault.jpg'
using php I'd like to replace hqdefault.jpg with maxresdefault.jpg
so the new thumbnail would look something like this:
$hq_thumbnail_url = 'http://i2.ytimg.com/vi/552yWya5RgY/maxresdefault.jpg'
Is this possible?
str_replace() is probably your most simple approach...
$thumbnail_url = 'http://i2.ytimg.com/vi/552yWya5RgY/hqdefault.jpg';
$hq_thumbnail_url = str_replace('hqdefault.jpg', 'maxresdefault.jpg', $thumbnail_url);
Hope this helps!
Here's another way to do it, and it will work even if hqdefault.jpg isn't at the end of the url :
$url = 'http://i2.ytimg.com/vi/552yWya5RgY/hqdefault.jpg'; // Url you want to change
$newImage = 'newimage.jpg'; // New filename
$splitUrl = explode('/', $url); // Split the url at each '/' occurence
$splitUrl[5] = $newImage; // Change the old filename (hqdefault.jpg) with the new one
$newUrl = implode('/',$splitUrl); // Reform the url, but this time, with the new filename.
echo $newUrl; // Here's the modified url
Related
I am going to make a URL checking system.
I have this URL
https://lasvegas.craigslist.org/mob/6169799901.html
Now I want to make this URL like this
https://lasvegas.craigslist.org/search/mob?query=6169799901
how can I do it using PHP?
Since I ended up (maybe?) solving it anyways, here's one method using URL/path parsing:
$url = 'https://lasvegas.craigslist.org/mob/6169799901.html';
$parsed = parse_url($url);
$basepath = pathinfo($parsed['path']);
echo $parsed['scheme'].
"://".
$parsed['host'].
"/search".
$basepath['dirname'].
"?query=".
$basepath['filename'];
Formatted for readability.
https://3v4l.org/E6Y54
Try this
$url = "https://lasvegas.craigslist.org/mob/6169799901.html";
$id = substr($url, strrpos($url, '/') + 1);
$id = str_replace(".html","",$id);
$result = "https://lasvegas.craigslist.org/search/mob?query=".$id;
echo $result;
I am having a web service which returns some images url, the service is working fine but the issue is that the url is not coming correctly.
My code is as given below
$obj->book_title = "test.jpg";
$url = "http://www.example.com/uploads/books/thumb/".$obj->book_title;
the above code outputs as
http:\/\/www.example.com\/uploads\/books\/thumb\/test.jpg
Can anyone please tell me some solution for this
the code listed should not do that, there is something else in your code that makes it output like that... you could try this
$obj->book_title = "test.jpg";
$url = "http://www.example.com/uploads/books/thumb/".$obj->book_title;
$url = str_replace("\\", "", $url);
echo $url;
I'm trying to change a value in a string that's holding my current URL. I'm trying to get something like
http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory
to look like
http://myurl.com/test/end.php?req=&srclang=english&destlang=english&service=MyMemory
replacing begin.php for end.php.
I need the end.php to be stored in a variable so it can change, but begin.php can be a static string.
I tried this, but it didn't work:
$endURL = 'end.php';
$beginURL = 'begin.php';
$newURL = str_ireplace($beginURL,$endURL,$url);
EDIT:
Also, if I wanted to replace
http://myurl.com/begin.php?req=&srclang=english&destlang=english&service=MyMemory
with
http://newsite.com/end.php?req=&srclang=english&destlang=english&service=MyMemory
then how would I go about doing that?
Assuming that you want to replace the script filename of the url, you can use something like this :
<?php
$endURL = 'end.php';
$url ="http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory";
$pattern = '/(.+)\/([^?\/]+)\?(.+)/';
$replacement = '${1}/'.$endURL.'?${3}';
$newURL = preg_replace($pattern , $replacement, $url);
echo "url : $url <br>";
echo "newURL : $newURL <br>";
?>
How do you want them to get to end.php from beigin.php? Seems like you can just to a FORM submit to end.php and pass in the variables via POST or GET variables.
The only way to change what page (end.php, begin.php) a user is on is to link them to another page from that page, this requires a page refresh.
I recently made a PHP-file for this, it ended up looking like this:
$vars = $_SERVER["QUERY_STRING"];
$filename = $_SERVER["PHP_SELF"];
$filename = substr($filename, 4);
// for me substr removed 'abc/' in the beginning of the string, you can of course adjust this variable, this is the "end.php"-variable for you.
if (strlen($vars) > 0) $vars = '?' . $vars;
$resultURL = "http://somewhere.com" . $filename . $vars;
I have the following url
/index.php?option=com_zoo&task=item&item_id=292&Itemid=283
What I want to do to replace item_id's value with a variable. I have been checking a couple of php functions like split and parse_str, but I do not know how I to get it to work.
$url = '/index.php?option=com_zoo&task=item&item_id=292&Itemid=283';
$query = explode('?', $url); // Split the URL on `?` to get the query string
parse_str($query[1], $data); // Parse the query string into an array
echo $data['item_id']; // 292
$newValue = 300;
$data['item_id'] = $newValue; // Replace item_id's value
$url = $query[0].'?'.http_build_query($data); // rebuild URL
echo $url; // '/index.php?option=com_zoo&task=item&item_id=300&Itemid=283";
Try the str_replace function. If you have your URL stored in the variable $url and the variable you want to replace Itemid stored in $ItemID:
$url = str_replace("Itemid", $ItemID, $url);
*this is the exact way of doing it *
<?php
$url = '/index.php?option=com_zoo&task=item&item_id=292&Itemid=283';
$explodeData =parse_url($url);
$dataReplace = str_replace('item_id','replacedvariable',$explodeData['query'],$count);
$changedUrl = $explodeData['path']."?".$dataReplace;
echo $changedUrl;
?>
I have a code download image from link http://www.bitrepository.com/download-image.html
When start is link format: <img src='test[1].jpg'>
But when download this link is link become <img src='test3%5B1%5D.jpg'>
How to fix it?
code here
<?php
include_once 'class.get.image.php';
// initialize the class
$image = new GetImage;
$image->source = 'http://test.com/test[1].jpg';
$image->save_to = 'images/'; // with trailing slash at the end
$get = $image->download('gd'); // using GD
if($get)
{
echo 'The image has been saved.';
}
?>
Try this.
On this line
$image->source = 'http://test.com/test[1].jpg';
Changed to
$image->source = htmlspecialchars_decode('http://test.com/test[1].jpg');
Look up urldecode in php to change the encoded values back to brackets