Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
if i output my URI i also get the folder in which it is located at.
echo $_SERVER['REQUEST_URI']; //outputs /abcgetoutofjail/admin.php?make_account
im on localhost right now, and the website is under folder in htdocs abcgetoutofjail
i need only admin.php?make_account
i need the last part of the url in any way
how can i achieve that with either another way of getting uri or using a string function to cut SERVER URI
function DescriptionAndTitle($uri)
{
echo $uri;
}
In Php, you can achieve this using explode & count function.
$uri = '/some-dir/yourpage.php?q=bogus&n=10';
$uri = explode('/', $uri);
$len = count($uri);
print $uri[$len-1];
To get the last part of a URL I usually do something like the following:
$url = parse_url($_SERVER['REQUEST_URI']);
$partsofurl = explode("/", $url['path']);
$partyouwant = end($partsofurl);
$needed_part = end(explode('/', $_SERVER['REQUEST_URI']));
I believe this is what you're looking for:
$dir = explode('/', dirname($_SERVER['PHP_SELF']));
$dir = '/'.end($dir);
echo $dir;
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a variable $TYPE=K180M-2
I need to "extract only the part until the dash (K180M) in a new variable.
How can I do it?
You may try:
$arr=explode("-", $TYPE);
$arr[0] will give you the desired result.
<?php
$newType = substr($TYPE, 0, stripos($TYPE, '-'));
?>
Will work.
You can use explode :
<?php
$K180M = '45';
$TYPE = 'K180M-2';
$var = explode('-',$TYPE,2);
$$var[0]; // this is your new variable named $K180M
var_dump($$var[0]); // result 45
If you prefer a regular expression, use preg_replace()
// Delete the dash and anything following it:
$type_without_dash = preg_replace("/-.*/", "", $TYPE);
Simply use
$value=strstr($TYPE,'-',TRUE);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi all i am looking for a simple way to check if a string equals an url like this:
http://youtu.be/WWQZ046NeUA
To convert it to a proper youtube url like this:
http://www.youtube.com/watch?v=WWQZ046NeUA
If not to leave it alone, what's the simplest way to do it in php?
You can use this preg_replace call:
$u = 'http://youtu.be/WWQZ046NeUA';
$r = preg_replace('~^https?://youtu\.be/([a-z\d]+)$~i', 'http://www.youtube.com/watch?v=$1', $u);
str_replace should work wonders.
$url = ''; //url you're checking
$ytshorturl = 'youtu.be/';
$ytlongurl = 'www.youtube.com/watch?v=';
if (strpos($url,$yturl) !== false) {
$url = str_replace($ytshorturl, $ytlongurl, $url);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I search in internet and i found this code for find domain name
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs))
{
return $regs['domain'];
}
return false;
}
Its works for http://www.google.com or http://www.google.co.uk
But its not working for test.web.tv.
Anybody can help me ?
How i can find min domain ?
Thanks
The function parse_url() requires a valid URL. In this case test.web.tv isn't valid, so parse_url() won't give you the expected results. In order to get around this, you could first check if the URL has the http:// prefix, and if it doesn't, manually prepend it. That way, you can get around the limitation of parse_url().
However, I think it'd be better to use the following function.
function getDomain($url)
{
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
$domain = implode('.', array_slice(explode('.', parse_url($url, PHP_URL_HOST)), -2));
return $domain;
}
Explanation:
The given URL's passed to parse_url() with the PHP_URL_HOST flag and the full host is obtained
It's exploded with . as a delimiter
The last two pieces of the array is sliced -- ie. the domain name
It's joined back using implode()
Test:
echo getDomain('test.web.tv');
Output:
web.tv
Demo!
Note: It's a modified version of my own answer here combined with Alix's answer here.
This function currently doesn't work for .co.uk domain extensions -- you can easily add a check and change the array_slice function accordingly.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string : http://www.mywebsite/456754567/531613490.htm?menu=contact
I want to get the value between "/" and ".htm". Here : 531613490
Any idea ?
if ( preg_match('~[^/]+(?=\.htm)~', $string, $matches) ) {
echo $matches[0];
}
Here's a demo: http://codepad.viper-7.com/5kjEj6
I don't know how flexible you want your script to be, but here's my try:
It always takes the last part of the path
It won't fail for http://www.mywebsite/456754567.htm/531613490.htm?menu=contact' as Joseph Silber's solution does.
<?php
$path = parse_url('http://www.mywebsite/456754567/531613490.htm?menu=contact', PHP_URL_PATH);
$pathParts = explode('/', $path);
$fullFilename = array_pop($pathParts);
// better use something like lastIndexOf(), this won't fail for 'xxx.abc.htm'
$filename = substr($fullFilename, 0, strpos($fullFilename, '.'));
var_dump( $filename );
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an SMF website and i'm actually trying to get some header information which includes the title of a particular thread, the url but i've been finding it difficult to get the unique link affixed to the url using PHP.
Here's the url: http://example.com/index.php?topic=6449.msg6858
I'm actually looking for a way to extract the number 6449, I've tried to use the php GET function but it doesn't work.
$parts = explode('.', $_GET['topic']);
echo $parts[0];
// PHP 5.4+
echo explode('.', $_GET['topic'])[0];
See it in action
This would work, too
echo (int) $_GET['topic'];
See it in action
You want to use a combination of substr and strpos (to find the first occurence of a period)
$number = substr($_GET['topic'], 0, strpos($_GET['topic'], '.'));
// 6449
$arr = array();
if (preg_match("/([\\d]+)([.]{1}msg[\\d]+)/", $_GET["topic"], $arr) == 1) {
echo $arr[1];
} else {
trigger_error("not found", E_USER_ERROR);
}