Regex split by character and capture - php

I'm looking for a regular expression to split and capture a url path eg with example.com/param1/param2/param3 I would like param1, param2 and param3 to be captured. There could be an unknown number of parameters. This will be used with PHP's preg_match. Can this be done?
EDIT:
This will be used with PHP's preg_match, because I am using it as a Zend Router Rule. Can this be done?

You don't need regex if it's always going to be a /, you could just explode it into an array:
$array = explode('/', $url);

Depends on if it allows it
(/[^/]*)+
That'll give multiple matches /param1, /param2, /param3
Then just strip the slashes

Related

Extract only numbers from link with codeception

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"

Get last parameter from the URL

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

Need a regular expression to capture url path

I am using PHP, and I have been trying to create a regular expression pattern to capture part of URL path, but to no avail.
The possible URL path could be any of these:
"product/zzz"
"yyyyyyyy/product/zzz"
"xxxxx/yyyyyyyy/product/zzz"
"xxxxx/yyyyyyyy/.../product/zzz" (... means other possible words)
what I need to capture is the part before "product".
for the first case, the result should be an empty string.
for the rest, they are "yyyyyyyy", "xxxxx/yyyyyyyy" and "xxxxx/yyyyyyyy/..."
Can anyone here give me hint? thanks!
PS.
It looks like the part I wanted is a repetition of same pattern "xxxx/". but I am not good at using group of regex.
Update:
I probably found a solution, by capturing pattern "xxx/" with zero or more repetitions: "([^/]+/)*"
so the full regex should be "(([^/]+/)*)product/([^/]+)"
#SERPRO: it passed the test in your "Live RegExp".
Hope it is helpful.
I would use parse_url():
$path = parse_url($url, PHP_URL_PATH);
// Deal with $path to figure out what's after '/product/'
This should work for you:
#(.*?)/?product.*\b#
You can see an example of result strings here:
http://xrg.es/#5awa10
This should do it:
^(.*[^/]|)/*product/[^/]+/*$
It will also allow an arbitrary number of slashes at the end of the path.
The part inside parentheses is your result.

PHP regex pattern to return N slugs/chunks (separated or split on the "/" char.) as matches from a "friendly" URL?

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.

help me understand PHP's preg_replace

I have this link:
http://mysite/myfolder/it/my-keywords.html and want to replace /it/ with /es/ (2 letter country codes)
I could explode() with "/" delimiter but would like to understand if preg_replace would be better.
tried:
preg_replace("/\/([a-z]{2})/\/", $link, $country);
EDIT
answer:
preg_replace("/\/[a-z]{2}\//", "/$country/", $link);
preg_replace is like a swiss army knife. preg rather than ereg means it uses perl compatible regular expressions. It matches first param (regex), replaces with second param (string) in third param (string).
Regular expressions are optimized for efficiency by using search tree cutoff techniques etc... so are generally efficient alternative method.
This should do what you want.
preg_replace("/\/it\//","/es/","http://mysite/myfolder/it/my-keywords.html")
preg_replace is useful if you only know a part of string that you want to match, but others parts are variable. In your case the /it/ folder is already unique enough so that a static replacement would work. Use str_replace instead:
$url = str_replace("/it/", "/es/", $url);

Categories