PHP preg_match not working perfectly - php

In my code I am trying to extract url only from my string using preg_match() function but its not working properly I tried all possible references but nothing works for me.
Here is the string example
FileID = "http://downloader.example.com/myfile/Josh.mp4";
My Expectation
http://downloader.example.com/myfile/Josh.mp4
Here is my php code
<?php
$stringURL = "FileID = \"http://downloader.example.com/myfile/Josh.mp4\";";
preg_match('/FileID(.*)=(.*)"(.*)"/U', $stringURL,$matches);
if (isset($matches[0])) {
$myurl = $matches[0];
} else {
$myurl = "http://";
}
echo $myurl;
?>

Why not it like this?
preg_match('/https?\:\/\/[^\" ]+/i', $stringURL,$matches);

Related

Internal Server Error when trying to check url

I'm trying to check the string after the last trailing slash in my URL.
My code is as follows:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$data = substr($url, strrpos($url, '/') + 1);
if($data == "dashboard") {
require_once VIEW_ROOT . '/cp/dashboard_view.php';
} else {
echo $data;
}
Once I go to http://MYURL/dashboard/in it should show in as the $data. Instead it gives me a 500 error.
You can simply use explode() function to break the string... .Or else $_SERVER[REQUEST_URI] shall give you the data after the host name...
But for the data after the last '/' explode function will work the best..
This will work.
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$x = explode('/',$url);
$data = $x[sizeof($x)-1];
echo $data;
You should try :
$url = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
You need to join
http:// string with $_SERVER[HTTP_HOST] and then $_SERVER[REQUEST_URI] using .(dot).

How to get a specific value from URL in PHP

I have a URL:
market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001
How can I get this value from above URL 000146132647632302db63d958690001
Can I use preg_match function or something else.
If you are receiving it as real URL than as simple as:
echo $_GET['trackingid'];
Else:
$queryArray = [];
$query = parse_url(
urldecode("market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001"),
PHP_URL_QUERY
);
parse_str($query, $queryArray);
echo $queryArray['trackingid'];
Live example
You can use regular expressions for that. Otherwise you cann access it direcly with $_GET
$url='market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001';
if(preg_match("/([^\?]*)\?trackingid%(d*)/",$url,$matches)){
echo $matches[1];
} else {
$_GET['trackingid']
}
1) One method is using explode().
$test = "market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001";
$url = explode("trackingid=",urldecode($test));
echo $url[1];
Working Demo : Click Here
2) Another is you can use preg_match() you can achieve it.
3) If you are getting it in url then get it using $_GET['trackingid'].
4) Using parse_str().
$url = urldecode("market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001");
parse_str($url, $tempArray);
echo $tempArray['trackingid'];

PHP: echo only the last part of a url, intead code printing the whole url [duplicate]

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];
?>

Extract the text from webpage

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]*)&?/;

Help w/ PHP Function (Whitespace)

I'm trying to use PHP to trim a YouTube URL down to the video ID. It's working, but it's also adding a lot of whitespace to the result. Does anyone know how I'd be able to fix this? Here's the setup:
Located in Wordpress single.php (finds attached YouTube URL from post meta):
<?php
$vidurl = get_post_meta($post->ID, "_videoembed", true );
$youtube_id = getYouTubeIdFromURL($vidurl);
echo $youtube_id;
?>
Here's what's executing the function:
//Get YT Video ID
function getYouTubeIdFromURL($url)
{
$url_string = parse_url($url, PHP_URL_QUERY);
parse_str($url_string, $args);
return isset($args['v']) ? $args['v'] : false;
}
Like I said, the function works fine, but it produces a bunch of white space. Say the video included in the meta is youtube.com/watch?v=1337&feature=player_embedded it'll return
" 1337 "
I appreciate all help in advance! I've had some really nice people help me on this site before.
Are you sure your get_post_meta function isn't returning a bunch of spaces with it? Can you do a var_dump on $vidurl? When I run the function you gave, the output looks just fine (http://www.ideone.com/LRoTd):
<?php
function getYouTubeIdFromURL($url)
{
$url_string = parse_url($url, PHP_URL_QUERY);
parse_str($url_string, $args);
return isset($args['v']) ? $args['v'] : false;
}
$theurl = "youtube.com/watch?v=1337&feature=player_embedded";
$youtubeid = getYouTubeIdFromURL($theurl);
echo "'$youtubeid'";
?>
Output:
'1337'

Categories