I have this code, the part I am looking for is the number from the url 1538066650683084805
I use this example
$tweet_url = 'https://twitter.com/example/status/1538066650683084805'
$arr = explode("/", $tweet_url);
$tweetID = end($arr);
Which works however sometimes on phones, When people copy and paste the url it has parameters on the end of it like this;
$tweet_url = 'https://twitter.com/example/status/1538066650683084805?q=2&t=1';
When a URL is exploded with the URL above the code doesn't work, how do I get the number 1538066650683084805 in both uses.
Thanks so much.
I would suggest using parse_url to get just the path, then separate that out:
$url = parse_url('https://twitter.com/example/status/1538066650683084805?q=2&t=1');
/*
[
"scheme" => "https",
"host" => "twitter.com",
"path" => "/example/status/1538066650683084805",
"query" => "q=2&t=1",
]
*/
$arr = explode("/", $url['path']);
$tweetID = end($arr);
I would explode first on the question mark and just look at the index 0 .. THEN explode the slash ...
$tweet_url = 'https://twitter.com/example/status/1538066650683084805?q=2&t=1';
$tweet_url = explode('?', $tweet_url)[0];
$arr = explode("/", $tweet_url);
$tweetID = end($arr);
If the question mark does not exist -- It will still return the full URL in $tweet_url = explode('?', $tweet_url)[0]; so it's harmless to have it there.
And this is just me .. But I would write it this way:
$tweet_url = 'https://twitter.com/example/status/1538066650683084805?q=2&t=1';
$tweetID = end(
explode("/",
explode('?', $tweet_url)[0]
)
);
echo $tweetID . "\n\n";
Related
I want to extract all used parameters of a link as a text string. Example:
$link2 = http://example.com/index.html?song=abcdefg;
When using the above link $param should give out all the parameters '?song=abcdefg'. Unfortunately I do not know the id index.html nor the parameters and their respective data values.
As much as I am informed there is the function $_GET, which creates an array, but I need a string.
You can use parse_url:
$link2 = 'http://example.com/index.html?song=abcdefg';
$param = '?' . parse_url($link2, PHP_URL_QUERY);
echo $param;
// ?song=abcdefg
Many librairies exist to parse url, you can use this one for an exemple :
https://github.com/thephpleague/uri
use League\Uri\Schemes\Http as HttpUri;
$link2 = 'http://example.com/index.html?song=abcdefg';
$uri = HttpUri::createFromString($link2);
// then you can access the query
$query = $uri->query;
You also can try this one :
https://github.com/jwage/purl
A weird way to do this is
$link2 = 'http://example.com/index.html?song=abcdefg';
$param = strstr($link2, "?");
echo $param // ?song=abcdefg
strstr($link2, "?") will get everything after the first position of ?; including the leading ?
you can loop over the get array and parse it into a string:
$str = "?"
foreach ($_GET as $key => $value) {
$temp = $key . "=". $value . "&";
$str .= $temp
}
rtrim($str, "&")//remove leading '&'
You can use http_build_query() method
if ( isset ($_GET))
{
$params = http_build_query($_GET);
}
// echo $params should return "song=abcdefg";
I have a strings like this:
index.php?url=index/index
index.php?url=index/index/2&a=b
I'm trying to get this part of string: index/index or index/index/2.
I have tried parse_str function but not successful.
Thanks.
You should be able to use $_SERVER['QUERY_STRING'] as shown below:
$url_params = $_SERVER['QUERY_STRING']; // grabs the parameter
$url_params = explode( '/', $url_params ); // seperates the params by '/'
which returns an array
Example index.php?url=index/index2 now becomes:
$url_params[ 0 ] = index;
$url_params[ 1 ] = index2;
Without more info:
// Example input
$input = "index.php?url=index/index/2&a=b";
$after_question_mark = explode("=",$input)[1];
$before_ampersand = explode("&",$after_question_mark)[0];
$desired_output = $before_ampersand; // 'index/index/2'
More resilient option:
// Example input
$input = "index.php?url=index/index/2&a=b";
$after_question_mark = explode("=",$input)[1];
if (strstr($after_question_mark, "&")){
// Check for ampersand
$before_ampersand = explode("&",$after_question_mark)[0];
$desired_output = $before_ampersand; // 'index/index/2'
} else {
// No ampersand
$desire_output = $after_question_mark;
}
$url = "index.php?url=index/index/2&a=b";
$query = parse_url($url, PHP_URL_QUERY);
$output = substr($query, $strpos($query, "=") + 1);
output: index/index/2&a=b
This will get you everything after the question mark
You can get more info on parse_url
or
$url = "index.php?url=index/index/2&a=b";
$output = substr($url, strpos("=") +1, $strrpos($url, "&") - strlen($url));
output: index/index/2
You dont need to break into an array to create overhead if you really just want to get a substring from a string
this will return false on no query string
URL of my site is:
http://mc.net46.net/ + folderName + fileName
For example:
http://mc.net46.net/mc/file01.php
http://mc.net46.net/mx/file05.php
folderName is always two characters long.
$address = 'http://mc.net46.net'.$_SERVER["REQUEST_URI"];
result: http://mc.net46.net/mc/file01.php - ok
$fname = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
result: file01.php - ok
Two questions:
Is this the correct way to get $address and $fname ?
How to get folderName?
Try this for another way to get your dynamic file names:
<?php
$fname = "http://mc.net46.net/mc/file01.php";
OR
$fname = $_SERVER["REQUEST_URI"];
$stack = explode('/', $fname);
$ss = end($stack);
echo $ss;
?>
Here for $fname you can use this $fname = explode('/', $_SERVER["REQUEST_URI"]);
Getting the address looks correct to me. However, you can get the $fname and the folder name easily using explode, and array_pop
$stack = explode('/', $_SERVER["REQUEST_URI"]);
$fname = array_pop($stack);
$folderName = array_pop($stack);
EDIT:
Explaining how does this work: the explode function will split the URI into ['', 'mc', 'file01.php'] for example. Now the function array_pop takes out the last element ($fname = 'file01.php') from the array, that means after the first call the array will be ['', 'mc'], and repeating the same action in the second call will will take out ($folderName = 'mc') as it will be the last element in the array and leave [''].
Use basename
$fname = basename("http://mc.net46.net/mc/file01.php")
RESULT = file01.php
DEMO
try
function getUriSegment($n) {
$segs = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
return count($segs)>0 && count($segs)>=($n-1)?$segs[$n] : '';
}
// if the url is http://www.example.com/foo/bar/wow
echo getUriSegment(1); //returns foo
echo getUriSegment(2); //returns bar
for more :- http://www.timwickstrom.com/server-side-code/php/php-get-uri-segments/
I have the following path for example:
/Test1/Test2/Test3
Sometimes this path can be for example:
/Test1/Test2/Test3/Test4/Test5 and so on...
What I would like to do is take this unknown path and translate it into sections which will ultimately result in a navigation URL such as:
/Test1
/Test1/Test2
/Test1/Test2/Test3
and so on...
It's difficult to supply you with any code examples because many of the things I have attempted have resulted in no good results.
I assume I need to explode() the path using / as the delimiter and then splice it together somehow. I'm really at a loss here.
Does anyone have any suggestions I can try?
<?php
$path = '/Test1/Test2/Test3/Test4/Test5';
$explode = explode('/', $path);
$count = count($explode);
$res = '';
for($i = 1; $i < $count; $i++) {
echo $res .= '/' . $explode[$i];
echo '<br/>';
}
Returns:
/Test1
/Test1/Test2
/Test1/Test2/Test3
/Test1/Test2/Test3/Test4
/Test1/Test2/Test3/Test4/Test5
Here is how you get your array segments:
$path = '/Test1/Test2/Test3/Test4/Test5'; // or whatever your path is
$segments = explode('/', ltrim('/',$path));
If I understand you, then what you want to do is to build an array that is like
Array(
[0] => '/Test1'
[1] => '/Test1/Test2'
...
)
So you could just loop through your array and build up this new array
$paths_from_segments = array();
$segment_count = count($sgements);
$path_string = '';
foreach($sgement as $segment) {
$path_string .= '/' . $segment;
$paths_from_segments[] = $path_string;
}
var_dump($paths_from_segments);
Not exactly what you mean by "splice it together", but from the sounds of it you're looking for PHP's implode(), which is explode() in reverse.
explode("/", "test1/test2");
// result:
// Array
// (
// [0] => test1
// [1] => test2
// )
implode("/", Array("test1", "test2"));
// result:
// "test1/test2"
Suppose I have the URL look like: http://www.example.com/category/product/htc/desire, I used $_SERVER['REQUEST_URI'] to get /category/product/htc/desire, how can I convert this "/category/product/htc/desire" to array like:
array
(
[0] => category
[1] => product
....
)
Thanks
$array = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
<?php
$url = "/category/product/htc/desire";
$pieces = explode("/", substr($url,1));
print_r($pieces);
?>
obviously $url would be the $_SERVER['REQUEST_URI']
output, see here: http://codepad.org/lIRZNTBI
use explode function
$list = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
Have a look at PHP strtok function
You can do something like that :
$string = "/category/product/htc/desire";
$arr = aray();
$tok = strtok($string, "/");
while ($tok !== false) {
arr[]= $tok:
$tok = strtok(" \n\t");
}