I want to get the last parameter from the following type of structure:
$current_url = "/wp/author/admin/1";
So, from above url I will like to get "1"
The following code will return it correctly, but here I'm specifying the exact position of the variable. How can I get the last parameter without specifying its position (eg. no matter how many parameters are in the URL, just get the last one):
$parts = explode('/', $current_url);
var_dump($parts[4]);
I would suggest using a regular expression for this, as you can do quite a few nice things, e.g. also allow URLs that end in /:
if (!preg_match('/\/([^\/]*)\/?$/', $current_url, $matches)
// do something if the URL does not match the pattern
$lastComponent = $matches[1];
What's happening here? The regular expression matches if it can find a forward slash (the \/) followed by any number of characters that are not slashes (the ([^\/]*)), which may then optionally be followed by another slash (the \/?), and then arrives at the end of the string (the $).
The function returns a value that evaluates to false if the regular expression did not match, so you are prepared for garbage input and may emit a warning if appropriate. Notice the parentheses in ([^\/]*), which will take all the characters mathced here (everything from the slash to the end of the input string or the last slash), and put it into its own match ($matches[1]).
I recommend you try regexpal.com if you want to debug and check your regular expressions. They are very powerful tools and quite underused in programming. Especially in PHP, where you get nice functions for them (e.g. preg_match, preg_match_all, and preg_match_split).
after you explode the array use the end() function. That will always grab the last element in the array.
http://us1.php.net//manual/en/function.end.php
I'm sure there are other methods, I would use array_pop
$parts = explode('/', $current_url);
var_dump(array_pop($parts));
http://php.net/manual/en/function.array-pop.php
"array_pop() pops and returns the last value of the array, shortening the array by one element."
but the last note is important as it affects the contents of $parts array
$parts = explode("/", $url);
echo end($parts);
Related
I have this link, and i need to work only with the numbers from that link.
How would i extract them?
I didn't find any answer that would work with codepcetion.
https://www.my-website.com/de/booking/extras#tab-nav-extras-1426
I tired something like this.
$I->grabFromCurrentUrl('\d+');
But i won't work.
Any ideas ?
Staying within the framework:
The manual clearly says that:
grabFromCurrentUrl
Executes the given regular expression against the current URI and
returns the first capturing group. If no parameters are provided, the
full URI is returned.
Since you didn't used any capturing groups (...), nothing is returned.
Try this:
$I->grabFromCurrentUrl('~(\d+)$~');
The $ at the end is optional, it just states that the string should end with the pattern.
Also note that the opening and closing pattern delimiters you would normally use (/) are replaced by tilde (~) characters for convenience, since the input string has a great chance to contain multiple forward slashes. Custom pattern delimiters are completely standard in regexp, as #Naktibalda pointed it out in this answer.
You can use parse_url() to parse entire URL and then extract the part which is most interested for you. After that you can use regex to extract only numbers from the string.
$url = "https://www.my-website.com/de/booking/extras#tab-nav-extras-1426";
$parsedUrl = parse_url($url);
$fragment = $parsedUrl['fragment']; // Contains: tab-nav-extras-1426
$id = preg_replace('/[^0-9]/', '', $fragment);
var_dump($id); // Output: string(4) "1426"
A variant using preg_match() after parse_url():
$url = "https://www.my-website.com/de/booking/extras#tab-nav-extras-1426";
preg_match('/\d+$/', parse_url($url)['fragment'], $id);
var_dump($id[0]);
// Outputs: string(4) "1426"
I seek your assistance once more with a small problem I am having, the solution is potentially obvious/looking at me in the face, but I have yet to resolve my issue.
I have the need to trim a string which happens to have a variable within it. (the string is actually a URL)
An example String/URL:
/portal/index.php?module=SerialDB&page=listing&returnmsg=3
&returnmsg=3 can be a range of numbers from 0 to 100, as well as, in some cases, text. This is the variable I need to trim as I am hoping to store the rest of the string/URL into a database. The following is the result I seek;
/portal/index.php?module=SerialDB&page=listing
I have tried the following code just to see if it could appropriate the function I require, but unfortunately it is more specific and won't trim unless it gets an EXACT match.
$str = "/portal/index.php?module=SerialDB&returnmsg=3";
echo $str . "<br>"; // Test to see full string
echo chop($str,"&returnmsg="); // Attempt to trim the string
If anyone is up to the task of assisting I would be greatly appreciative. As with all my questions, I would also like to understand what is happening as opposed to just being handed the code so I can use it confidently in the future.
Thanks once again guys :)
A quick way that doesn't depend on parameter order is just to take apart the pieces, pick out what you want, and then put them back together again (you can look up the various PHP functions for more details):
$urlParts = parse_url($url);
parse_str($urlParts['query'], $queryParts);
$returnMsg = $queryParts['returnmsg'];
unset($queryParts['returnmsg']);
$urlParts['query'] = http_build_query($queryParts);
$url = http_build_url($urlParts);
Simple. The concept is known as slicing.
$url = "/portal/index.php?module=SerialDB&page=listing&returnmsg=3";
$new_url = substr( $url, 0, strrpos( $url, '&') );
result is: /portal/index.php?module=SerialDB&page=listing
The substr() function returns part of a string. It takes three parameters. Parameter #1 is the string you are going to extract from, parameter #2 is where are you going to start from and parameter #3 is the length of the slice or how many characters you want to extract.
The strrpos() function returns the position/index of the last occurrence of a substring in a string. Example: if you have the string "zazz" the position of the last z will be returned. You can think of it as "string reverse position". This function accepts three parameters. I will only cover two as this is the number I used in this example. Parameter #1 is the string you are searching in, parameter #2 is the needle or what you are looking for, in your case the &. As I mentioned in the beginning of this paragraph, it returns the position in the form of an integer. In your case that number was 46.
So the strrpos() as the third parameter in substr() is telling it up to where to slice the string. Upon completion it returns the segment that you wanted to extract.
It would be helpful if you read the PHP Manual and looked over the available functions that might help you in the future. You can combine these functions in various ways to solve your problems.
http://php.net/manual/en/funcref.php
If returnmsg is always the last param (and not the first) in your url and if your url doesn't contain an anchor, so in short the param is always at the end, you can solve the problem with a simple explode:
$url = explode('&returnmsg=', $url)[0];
(you split the string with &returnmsg= and you keep the first part).
otherwise as suggested in comments you can use a regex replacement:
$url = preg_replace('~[?&]returnmsg=[^&#]*~', '', $url);
pattern details:
~ # pattern delimiter
[?&] # a literal "?" or a literal "&"
returnmsg=
[^&#]* # any characters except "&" or "#" zero or more times
~
(for the two ways, if the param is not present, the url stay unchanged.)
I don't weather it comes in starting or in end so if it comes in end then use the code below.
<?php
$url="/portal/index.php?module=SerialDB&page=listing&returnmsg=3";
$array= explode("&",$url);
$new_url="";
foreach($array as $p){
if(strpos($p,"returnmsg")===false){
$new_url .=$p."&";
}
}
echo rtrim($new_url, "&");
The above code is exploding the array & and then running a foreach loop to join them.
#Bobdye answer is also correct but there is a bit problem in that code, that wasn't running for me. Use the code below
<?php
$url="/portal/index.php?module=SerialDB&page=listing&returnmsg=3";
$urlParts = parse_url($url);
parse_str($urlParts['query'], $queryParts);
$returnMsg = $queryParts['returnmsg'];
unset($queryParts['returnmsg']);
$urlParts['query'] = http_build_query($queryParts);
$url = http_build_query($urlParts);
var_dump($url);
Hope this helps you
I'm looking for a regex pattern that will return N slugs/chunks (all pieces of the URL, separated or split on the "/" char.) as matches from a "friendly" URL.
The pattern should not include the domain or a leading slash.
Also, the pattern should work with an unknown number of slugs and/or slashes.
For example, some example URLs and desired returned slugs/chunks:
"" = array()
"foo/bar/" = array('foo', 'bar')
"foo/bar/baz" = array('foo', 'bar', 'baz')
"foo-bar/baz" = array('foo-bar', 'baz')
Finally, I need to pass this regex pattern preg_match (or similar) and have it return the results via the function's $matches parameter.
For example:
<?php preg_match($your_pattern, $friendly_url, $your_pattern_matches); ?>
... similar results can be prduced using explode().
This pattern is being used in a much more complex scenario than my little old example; requiring the use/forcing me to use regex patterns via preg_match for the solution. Basically, I'm passing preg_match a pattern of choice, which is why I need a regex pattern as opposed to simply using explode.
Your help is GREATLY appreciated!
Cheers!
First of all, check the manual of preg_split
$segments = preg_split('[/]', $uri, 0, PREG_SPLIT_NO_EMPTY);
If you insist on preg_match take a look on this:
$uri = '/foo-bar/baz';
preg_match_all('%[^/]+%', $uri, $matches);
print_r($matches);
Sounds like explode() would do the job without having to bother with regexes:
$matches = explode('/', $url);
Sorry but I don't think you can do what you want with preg_match.
After reading the documentation
You can see that preg_match will stop at the first match. You want an array of the matches in a friendly url however this can only be achieved by multiple matches , in order to store the values in an array OR by a single match which would capture the whole thing. Both of these cases do not fit you so I am afraid that you would have to use something else than preg_match.
In a url like the one below, I'd like to get the value of ProdId. The URL format will always be consistent, as will the parameter name, but the length of the value may change. It will always be numeric.
http://www.example.com/page.php?ProdId=2683322&xpage=2
Using PHP what's the fastest way to get it (I'll be processing 10,000's so speed is an issue)?
PHP has built-in functions for this. Use parse_url() and parse_str() together.
Pieced together from php.net:
$url = 'http://www.example.com/page.php?ProdId=2683322&xpage=2';
// Parse the url into an array
$url_parts = parse_url($url);
// Parse the query portion of the url into an assoc. array
parse_str($url_parts['query'], $path_parts);
echo $path_parts['ProdId']; // 2683322
echo $path_parts['xpage']; // 2
Try this regular expression:
^http://www\.example\.com/page\.php\?ProdId=(\d+)
Can't you use $_GET['ProdId']?
Try this function:
/https?:\/{2}(?:w{3}\.)?[-.\w][^\.]+\.{2,}\/ProdId=\d+\&xpage=\d+/
/^[^#?]*\?(?:[^#]*&)?ProdId=(\d+)(?:[#&]|$)/
And the same in English:
Match anything except ? or # (this will get us to the beginning of the query string or the hash part, whichever comes first)
Match the ? (if there was only a hash part, this will disqualify the match)
Optionally match anything (but not a #, in case there's a hash part) followed by &
Match your key value pair putting the value in a capturing subpattern
Match either the next param's &, the # or the end of the string.
I'm new at regular expressions and wonder how to phrase one that collects everything after the last /.
I'm extracting an ID used by Google's GData.
my example string is
http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123
Where the ID is: p1f3JYcCu_cb0i0JYuCu123
Oh and I'm using PHP.
This matches at least one of (anything not a slash) followed by end of the string:
[^/]+$
Notes:
No parens because it doesn't need any groups - result goes into group 0 (the match itself).
Uses + (instead of *) so that if the last character is a slash it fails to match (rather than matching empty string).
But, most likely a faster and simpler solution is to use your language's built-in string list processing functionality - i.e. ListLast( Text , '/' ) or equivalent function.
For PHP, the closest function is strrchr which works like this:
strrchr( Text , '/' )
This includes the slash in the results - as per Teddy's comment below, you can remove the slash with substr:
substr( strrchr( Text, '/' ), 1 );
Generally:
/([^/]*)$
The data you want would then be the match of the first group.
Edit Since you’re using PHP, you could also use strrchr that’s returning everything from the last occurence of a character in a string up to the end. Or you could use a combination of strrpos and substr, first find the position of the last occurence and then get the substring from that position up to the end. Or explode and array_pop, split the string at the / and get just the last part.
You can also get the "filename", or the last part, with the basename function.
<?php
$url = 'http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123';
echo basename($url); // "p1f3JYcCu_cb0i0JYuCu123"
On my box I could just pass the full URL. It's possible you might need to strip off http:/ from the front.
Basename and dirname are great for moving through anything that looks like a unix filepath.
/^.*\/(.*)$/
^ = start of the row
.*\/ = greedy match to last occurance to / from start of the row
(.*) = group of everything that comes after the last occurance of /
you can also normal string split
$str = "http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123";
$s = explode("/",$str);
print end($s);
This pattern will not capture the last slash in $0, and it won't match anything if there's no characters after the last slash.
/(?<=\/)([^\/]+)$/
Edit: but it requires lookbehind, not supported by ECMAScript (Javascript, Actionscript), Ruby or a few other flavors. If you are using one of those flavors, you can use:
/\/([^\/]+)$/
But it will capture the last slash in $0.
Not a PHP programmer, but strrpos seems a more promising place to start. Find the rightmost '/', and everything past that is what you are looking for. No regex used.
Find position of last occurrence of a char in a string
based on #Mark Rushakoff's answer the best solution for different cases:
<?php
$path = "http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123?var1&var2#hash";
$vars =strrchr($path, "?"); // ?asd=qwe&stuff#hash
var_dump(preg_replace('/'. preg_quote($vars, '/') . '$/', '', basename($path))); // test.png
?>
Regular Expression to collect everything after the last /
How to get file name from full path with PHP?