url path is coming wrongly in php - php

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;

Related

PHP fetch with file_get_contents() funtion

i want to get m3u8 link from this site on other php page
for example : this is the site : "view-source:http://lideo.ru/embed/9528"
i want to extract only this m3u8 to other php page : http://hls.lideo.ru/liveapp/09528_bd608448e6c4a218d16b1f7b4018f6e8/index.m3u8?tokenhash=TIai4tLuWZHdLOS5mjNOqw
I'm tried this code but seems not working
$url = "http://lideo.ru/embed/9528";
$contents = file_get_contents($url);
preg_match('#m3u8([^"]+)#',$contents,$rtmp);
$link = urldecode($link[0]);
echo $link;
Any help please because i'm not soo good in php :)
thanks
Try this:
$url = "http://lideo.ru/embed/9528";
$contents = file_get_contents($url);
preg_match('/(https?\:\/\/[^\']*\.m3u8\?[^\']*)/', $contents, $result);
$link = urldecode($result[0]);
echo $link;

Need to change url using php

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;

PHP replace the very last part of a url

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

Stripping url with preg_replace

I needed to strip the
http://www.
from a domain name and also anything following it such as
/example
so that i would just be left with yourdomain.com
I added the following code to a file:
$domain = HTTP_SERVER;
$domain_name = preg_replace('/^https?:\/\/(?:www\.)?/i', '', $domain);
But if i echo $domain_name I still get a url such as yourdomain.com/testsite
Can anyone see what i have done wrong here as it has not removed the /testsite and i thought i had got this right.
use this
$url = 'http://www.example.co.uk/directory/level1/last/page.html';
$parse= parse_url($url);
preg_match ("/\.([^\/]+)/", $parse['host'], $mydomain);
echo $mydomain[1];
This may be a hack that someone will disagree with, but i resolved the problem by using the following code.
$url = HTTP_SERVER;
$parse = parse_url($url);
$domain = $parse['host'];
$domain_name = preg_replace('/(?:www\.)?/i', '', $domain);
echo $domain_name;
If you can see a reason why this should not be used, please feel free to let me know. Always something new to learn :)

URL Replacement in PHP

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;

Categories