My page source have this html (js):
<script>
playlist.sources.push({
label: "480p",
source: "//zingtv-video-14.zadn.vn/Video480/2016/0418/7c/1f1fd73f7432d7524cb43b57da35d6df.mp4?authen=exp=1559094603~acl=1f1fd73f7432d7524cb43b57da35d6df~hmac=01da84812b2ef7d43e55be79fa3ef56e",
index: 1
});</script>
I want to get:
//zingtv-video-14.zadn.vn/Video480/2016/0418/7c/1f1fd73f7432d7524cb43b57da35d6df.mp4?authen=exp=1559094603~acl=1f1fd73f7432d7524cb43b57da35d6df~hmac=01da84812b2ef7d43e55be79fa3ef56e
My code :
<?php
// Check if the URL parameter for our proxy is set.
if (!empty($_GET['url'])) {
if (filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
$grabs = file_get_contents("{$_GET['url']}");
$grab = json_decode($grabs, true);
// I don't know what to do next
echo "{$video}" />";
} else {
echo "Given URL is not valid.";
}
} else {
echo "You need to specify the URL.";
}
?>
and show in the content page.
How do I solve this problem?
Here, we can try using preg_match_all with a simple expression:
source: "(.+)",
where our desired output is saved in a capturing group $1.
Test
$re = '/source: "(.+)",/m';
$str = '<script>
playlist.sources.push({
label: "480p",
source: "//zingtv-video-14.zadn.vn/Video480/2016/0418/7c/1f1fd73f7432d7524cb43b57da35d6df.mp4?authen=exp=1559094603~acl=1f1fd73f7432d7524cb43b57da35d6df~hmac=01da84812b2ef7d43e55be79fa3ef56e",
index: 1
});</script>';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
var_dump($matches[0][1]);
DEMO
I'm not quite sure if this file_get_content would work:
$str = file_get_contents('https://tv.zing.vn/video/id/IWZBFE8U.html?t=388');
$str = mb_convert_encoding($str, 'HTML-ENTITIES', "UTF-8");
var_dump($str);
Related
I'm trying to get the html5player.setVideoUrlHigh var from an external source that renders like this:
<script>
logged_user = false;
var static_id_cdn = 2;
var html5player = new HTML5Player('html5video', '38295'); //this is the video ID
if (html5player) {
html5player.setVideoTitle('video title goes here');
html5player.setVideoUrlHigh('https://random-prefix-here.site-url.com/videos/mp4/random-part-of-url');
...
}
</script>
Firstly, I do file_get_contents to get the embed code:
$videoID = 38295;
$url = file_get_contents('https://www.site-url.com/embedframe{$videoID}');
then, I look for a url match that contains specific parts like (site-url.com/videos/mp4/) to eco it, like this:
preg_match('/https:\/\/[^\"]*/\.site-url\.com\/videos\/mp4\/[^\"]*/', $url, $matches, PREG_OFFSET_CAPTURE);
$videoURL = ($matches[0][0]);
echo '=>' . $videoURL;
but this is not working... Where am I failing?
Please keep in mind that both random-prefix-here and random-part-of-url should be replaced by regex in a possible solution.
UPDATED
I already tried this also, witouth any results:
$url = 'http://www.site-url.com/embedframe38295/';
$content= file_get_contents($url);
preg_match_all('#html5player.setVideoUrlHigh\((.+?)\')', $url, $matches, PREG_OFFSET_CAPTURE);
$videoURL = ($matches[0][0]);
echo $videoURL;
I have long Text containg som html urls inside it from database .
im fetching it like that :
echo html_entity_decode($text);
and $text is like that in database:
$text = "this is my best <a href='http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere'>http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere</a> and some text here";
it echoes this :
this is my best http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere and some text here
But i want it display shorted text of the url and the href link is same like that :
this is my best http://www.website.com/andsoooooo... and some text here
How do i detect how long is it the link text and then short it automatically when fetching from database BUT the real link stayed as it is in href tag . thanks so much
EDIT im not looking to short the $text im looking to short the link inside the $text
Use substr():
$text = "this is my best <a href='http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere'>http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere</a>";
// Gets the whole tag
// e.g., "Text"
$linkTag = getTagFromYourUglyAssString($text, "a");
// Gets the tag's text value
// e.g., "Text"
$linkTagTextOnly = getTagTextFromYourUglyAssString($text, "a");
// Replace $linkTag with $linkTagTextOnly, whichever string you prefer ot shorten
$shortenedText = substr($linkTagTextOnly,0,10).'...';
echo $shortenedText;
function getTagFromYourUglyAssString($string, $tagname) {
$pattern = "/<$tagname ?.*>.*<\/$tagname>/";
preg_match($pattern, $string, $matches);
return array_merge($matches);
}
function getTagTextFromYourUglyAssString($string, $tagname) {
$pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
EDIT: Actually, I think this is what you want:
$text = "this is my best <a href='http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere'>http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere</a>";
$shortenedLink = replaceTagTextFromYourUglyAssString($text, "a");
echo $shortenedLink;
// Should give:
// <a href="$text = "this is my best <a href='http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere">
// http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_chara...
// </a>
function replaceTagTextFromYourUglyAssString($string, $tagname) {
$pattern = "/(<$tagname ?.*>)(.*)(<\/$tagname>)/";
preg_match($pattern, $string, $matches);
if (count($matches) > 0) {
return $matches[0].substr($matches[1],0,10).'...'.$matches[2];
} else {
// do something else, 'cos no match found
}
}
This question already has answers here:
Get only filename from url in php without any variable values which exist in the url
(13 answers)
Closed 7 years ago.
I wanted to print the last characters after "/" in a url. but instead it is printing the whole url, I expected the output to be just "index.php" instead it is printing out the whole url.
How should i go about doing it right?
$data = $_SERVER['REQUEST_URI'];
$whatIWant = substr($data, strpos($data, "/") + 1);
echo $whatIWant;
You can see it here
You should get the actual link by
<?php
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$getpath=explode("/",$actual_link);
echo end($getpath);
?>
Short Explanation :
Step 1 : Get the url by
http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]
Step 2 : Explode with slash
explode("/",$actual_link)
Step 3 : Get the last part
end($getpath);
Try this..
<?php
$data = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$whatIWant = explode("/",$data);
echo end($whatIWant);?>
You can also try it this way using strrchr :
$url = 'http://spiritofethiopia.com/test/test/test/index.php';
$str = substr(strrchr($url, '/'), 1);
echo $str;
strrchr — Find the last occurrence of a character in a string.
You can use preg_match whith a correct RegExp to capture the end of the URL.
<?php
$data = $_SERVER['REQUEST_URI'];
if(preg_match('#/([^/]*?)$#', $data, $matches) == 1) {
echo $matches[1];
}
else {
// Should not happen
/*
* Throw exception
*/
}
?>
Try strripos instead of strpos, it may works
<?php
$data = $_SERVER['REQUEST_URI'];
$whatIWant = substr($data, strripos($data, "/") + 1);
echo $whatIWant;
?>
Try This:
working solution,
<?php
$url = 'http://test/test/test/index.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-1];
?>
In this test.php page i have this line of text
server=span.growler.ro&provider=-1&providersSerial=4&country=RO&mobile=0&token=eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb
in this other test1.php?id=token page i have this php code runing
<?php
$Text=file_get_contents("./test.php");
if(isset($_GET["id"])){ $id = $_GET["id"];
$regex = "/".$id."=\'([^\']+)\'/";
preg_match_all($regex,$Text,$Match);
$fid=$Match[1][0];
echo $fid; } else { echo ""; } ?>
i need only the token
eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb
to be show on test1.php?id=token
if in test.php the token looks like this
token='eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb'
it works.
i needet to work from onother web page
$str = 'server=span.growler.ro&provider=-1&providersSerial=4&country=RO&mobile=0&token=eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb';
parse_str($str, $vars);
$token = $vars['token'];
using with preg_match will help you .
$string ='server=span.growler.ro&provider=-1&providersSerial=4&country=RO&mobile=0&token=eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb';
preg_match('/token=([a-f0-9]+)/i',$string,$matches);
echo $matches[1];
this will return you :
'eae5b2c50c123425d9351d8c8ee80b9a27ca3d69f15a669454b937eb'
I'd recommend you to use preg_match instead of preg_match_all
Try this regex:
$regex = "/&?token=([a-f0-9]*)&?/;
I have a coupon site that display store urls on my store pages. What I want is for only .com at end of each store without showing http:// variations in the beginning
here is my code that displays a store url and I just want domain.com to be displayed instead of http://www.domain.com, also may show as http://domain.com
<p class="store-url"><a href="<?php echo $url_out; ?>" target="_blank"><?php echo $stores_url; ?>
It displays like this because of this function
<div class="store">
<?php // grab the store meta data
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
$stores_url = esc_url(get_metadata(APP_TAX_STORE, $term->term_id, 'clpr_store_url', true));
$dest_url = esc_url(get_metadata(APP_TAX_STORE, $term->term_id, 'clpr_store_aff_url', true));
// if there's a store aff link, then cloak it. else use store url
if ($dest_url)
$url_out = esc_url(home_url(CLPR_STORE_REDIRECT_BASE_URL . $term->slug));
else
$url_out = $stores_url;
?>
What can be done................
Quick and dirty - to demonstrate the possible functions...
<?php
function cleanInputString($inputString) {
// lower chars
$inputString = strtolower($inputString);
// remove whitespaces
$inputString = str_replace(' ', '', $inputString);
// check for .com at the end or add otherwise
if(substr($inputString, -4) == '.com') {
return $inputString;
} else {
return $inputString .'.com';
}
}
// example
$inputStrings = array(
'xyzexamp.com',
'xyzexamp',
'xyz examp'
);
foreach($inputStrings as $string) {
echo('input: '. $string .'; output: '. cleanInputString($string) .'<br />');
}
?>
OUTPUT:
input: xyzexamp.com; output: xyzexamp.com
input: xyzexamp; output: xyzexamp.com
input: xyz examp; output: xyzexamp.com
The "right way" is probably to use the PHP URL-processing:
Break the URL up using http://php.net/manual/en/function.parse-url.php
Remove the scheme element of the resulting array using unset
Build it again using http://www.php.net/manual/en/function.http-build-url.php
This is what preg_replace was made for:
<?php
$http_url = 'http://www.somestore.com/some/path/to/a/page.aspx';
$domain = preg_replace('#^https?://(?:www\.)?(.*?)(?:/.*)$#', '$1', $http_url);
print $domain;
?>
This code will print out
somestore.com