How to get folder name from url address - php

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/

Related

seperate string and remove url parameters

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";

Need to copy a specific text generated on page to a string in PHP

I made a PHP script which is generating result "https://secure.nmi.com/api/v2/three-step/44505010"
44505010 is a number that changes every time. I need to create a $reference string which I can assign that dynamic number to $reference.
I tried this code but it doesn't seem to work. Can someone help me out?
$reference = getStr($page, 'https://secure.nmi.com/api/v2/three-step/','');
Thanks
Try this:
<?php
function getUrlPart($url = '') {
$urlarr = parse_url($url);
$split = explode('/', $urlarr['path']);
$c = 1;
while($split[count($split)-$c] == ''){
$c++;
}
return var_export($split[count($split)-$c], true);
}
$url = 'https://secure.nmi.com/api/v2/three-step/44505010//';
echo getUrlPart($url);
In this method you can get the route name at any depth, even with query parameters at the end.
$yourUrl = 'https://secure.nmi.com/api/v2/three-step/44505010';
$reference = (int) explode('/', $yourUrl)[6];
This assumes the general URL format does not change.

Separate name string into first and last name

If I use the same name for both the first and last name or use a ' my name splitting function is not working as intended. How can I make it work even if the string looks like:
Jeanne D'Arc
Lang Lang
The first will return the whole name in the $last_name string and the second one will return only one name in the $last_name string.
function split_name($name) {
$name = trim($name);
$last_name = (strpos($name, ' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name);
$first_name = trim( preg_replace('#'.$last_name.'#', '', $name ) );
return array($first_name, $last_name);
}
Thanks!
Like Sergio answer. Take a look what is already possible to do with PHP. explode do the same thing as your split_name.
Source:
http://php.net/manual/fr/function.explode.php
This worked for me! It is assuming that you only have one last name and that it's the last word of the string, but the user will be able to edit this anyway so it's just a starting point. Thank you guys.
function split_name($name) {
$parts = explode(" ", trim($name));
$num = count($parts);
if($num > 1)
{
$lastname = array_pop($parts);
}
else
{
$lastname = '';
}
$firstname = implode(" ", $parts);
return array($firstname, $lastname);
}

two variables values in one variable is possible?

My code:
$url = "http://www.google.com/test";
$parseurl = parse_url($url);
$explode = explode('.', $parseurl['host']);
$arrayreverse = array_reverse($explode);
foreach(array_values($arrayreverse) as $keyvalue) {
$result[] = $keyvalue;
}
$implode = implode('.', $result);
...
I'm interested to have values in one variable from $implode result and the result if isset the path from $parseurl... how can I do that ?
EDIT:
I want the values of $implode + and(if isset $parseurl['path']); to be in 1 variable but I can't figure out how to unite them.
Is ths what you want?
$newurl = $implode . (isset($parseurl['path']) ? $parseurl['path'] : '');
It uses the conditional operator to return either the path or an empty string, and concatenate that onto $implode.

PHP explode and set to empty string the missing pieces

What's the best way to accomplish the following.
I have strings in this format:
$s1 = "name1|type1"; //(pipe is the separator)
$s2 = "name2|type2";
$s3 = "name3"; //(in some of them type can be missing)
Let's assume nameN / typeN are strings and they can not contain a pipe.
Since I need to exctract the name / type separetly, I do:
$temp = explode('|', $s1);
$name = $temp[0];
$type = ( isset($temp[1]) ? $temp[1] : '' );
Is there an easier (smarter whatever faster) way to do this without having to do isset($temp[1]) or count($temp).
Thanks!
list($name, $type) = explode('|', s1.'|');
Note the order of arguments for explode()
list($name,$type) = explode( '|',$s1);
$type will be NULL for $s3, though it will give a Notice
I'm a fan of array_pop() and array_shift(), which don't error out if the array they use is empty.
In your case, that would be:
$temp = explode('|', $s1);
$name = array_shift($temp);
// array_shift() will return null if the array is empty,
// so if you really want an empty string, you can string
// cast this call, as I have done:
$type = (string) array_shift($temp);
There is not need to do isset since $temp[1] will exist and content an empty value. This works fine for me:
$str = 'name|type';
// if theres nothing in 'type', then $type will be empty
list($name, $type) = explode('|', $str, 2);
echo "$name, $type";
if(strstr($temp,"|"))
{
$temp = explode($s1, '|');
$name = $temp[0];
$type = $temp[1];
}
else
{
$name = $temp[0];
//no type
}
Maybe?

Categories