I'm trying to create a url handler and I tried to use preg_match for finding the variables.
So here is the preg_match that I get after the variable replacing of the url format (ex:post/%id%/%title%) -
preg_match("/post\/[0-9]+\/[a-z-א-ת-]+/i","post/5/lol",$match);
My problem that $match returns only this inside array -
"post/5/lol"
And not an array like that -
Array (
[0] => "post/5/lol",
[1] => "5",
[2] => "lol"
)
Can someone help me please to find out why it returns only one match?
Wrap each segment you wish to capture with parentheses, creating sub-expressions:
preg_match("/post\/([0-9]+)\/([a-z-א-ת-]+)/i", "post/5/lol", $match);
Related
I have a string with multiple tags in as so:
<item>foo bar</item> <item>foo bar</item>
I need to match each of these and they can be on new lines and add them to an array, it can't seem to match them though, I'm new to regex so I'm not understanding what is going wrong, an explanation would be great, thanks!
preg_match_all('/<item>(.*)<\/item>/',$content,$matches);
At the moment, it returns two empty index in the matches array.
I have also tried:
<item>([\s\S]*)<\/item>
This matches from the first tag until the very last one, so grabs everything essentially.
You can use this
preg_match_all('/<item>(.*?)<\/item>/',$content,$matches);
Result
Array
(
[0] => Array
(
[0] => <item>foo bar</item>
[1] => <item>foo bar</item>
)
[1] => Array
(
[0] => foo bar
[1] => foo bar
)
)
I only added ? to the regex, that looks for the nearest match and get it.
Read about lazy and greedy here: What do lazy and greedy mean in the context of regular expressions?
i have a problem with preg_match , i cant figure it out.
let the code say it :
function::wp_statistics_useronline::end
function::wp_statistics_visitor|today::end
function::wp_statistics_visitor|yesterday::end
function::wp_statistics_visitor|week::end
function::wp_statistics_visitor|month::end
function::wp_statistics_visitor|total::end
these are some string that run functions inside php;
when i use just one function::*::end it works just fine.
but when it contain more than one function , not working the way i want
it parse the match like :
function::wp_statistics_useronline::end function::wp_statistics_visitor|today::end AND ....::end
so basically i need Regex code that separate them and give me an array for each function::*::end
I assume you were actually using function::(.*)::end since function::*::end is never going to work (it can only match strings like "function::::::end").
The reason your regex failed with multiple matches on the same line is that the quantifier * is greedy by default, matching as many characters as possible. You need to make it lazy: function::(.*?)::end
It's pretty straight forward:
$result = preg_match_all('~function::(\S*)::end~m', $subject, $matches)
? $matches[1] : [];
Which gives:
Array
(
[0] => wp_statistics_useronline
[1] => wp_statistics_visitor|today
[2] => wp_statistics_visitor|yesterday
[3] => wp_statistics_visitor|week
[4] => wp_statistics_visitor|month
[5] => wp_statistics_visitor|total
)
And (for the second example):
Array
(
[0] => wp_statistics_useronline
[1] => wp_statistics_visitor|today
)
The regex in the example is a matching group around the part in the middle which does not contain whitespace. So \S* is a good fit.
As the matching group is the first one, you can retrieve it with $matches[1] as it's done after running the regular expression.
This is what you're looking for:
function\:\:(.*?)\:
Make sure you have the dot matches all identifier set.
After you get the matches, run it through a forloop and run an explode on "|", push it to an array and boom goes the dynamite, you've got what you're looking for.
I'm trying to get grouped matches from the following URI:
route: "/user/{user}/{action}"
input: "/user/someone/news"
What's the appropriate regex for this? I've been searching myself sour for the past couple of hours...
I've tried something like this, but no result :(
~\/app\/user\/(?P<user>[.*]+)\/(?P<action>[.*]+)~
I get the groups back in the matches array, but no results based on the input inside the groups.
Desired output:
Array
(
[0] => Array
(
[0] => "someone"
)
[user] => Array
(
[0] => "someone"
)
[1] => Array
(
[0] => "news"
)
[action] => Array
(
[0] => "news"
)
)
To clarify with an example:
My controller has the following route: /app/user/{username}/{action}
The request URI from the browser is: /app/user/john/news
How do I match that request URI against that route using a regex patter while catching the variables between the brackets?
/user/(?P<user>[^/]+)/(?P<action>[^/]+)
http://regex101.com/r/gL1aS2
Just to explain a couple problems with your original regex:
[.*]+ means a positive number of occurrences of a dot and an asterisk only, example: *.*.* or . or ......; [^/]+ describes a positive number of any characters but slashes.
No need to escape slashes, as they're not special characters when you're using ~ as delimiters.
Your regex also required /app at the beginning, which wasn't present in your string.
I've used preg_match() to check a string coming from an XML feed (ie: $resp = simplexml_load_file($API);) which returns upwards of 1000 items and with preg_match I've extracted a bit of data from each item which is stored in $matches but I don't know how to make use of what preg_match has stored in $matches
Here's what I've got and what I've tried.
Note: I have print_r($matches); just so I could see the results while modifying the preg pattern.
$matches;
preg_match('/(?<=\s|^)[a-zA-Z]{5,19} ?-?\d\d\d\d\d\d\d\d?*(?=\s|$)/', $Apples, $matches);
print_r($matches);
/*Note: $matches returns an array as such: Array ( [0] => Stringdata ) Array ( [0] => moreStringdata ) Array ( [0] => stillmoreStringData ) Array ( [0] => evenmoreStringData ) Array ( [0] => moreStringDataStill )... and I'm just wanting to use array[0] from each in the $results string which is output to the screen */
$results.= "<div class='MyClass'><img src=\"$linktopicture\">$matches</div>";
I Also tried $matches(), $matches[] and $matches[0] in the $results string but nothing works and since I don't know much about using arrays I thought I'd ask so if anyone wouldn't mind setting me straight with what is probably very elementrary I'd be most appreciative and I thank you all in advance.
Make sure to read the preg_match doc page to understand the way the function works.
Firstly, check to see whether preg_match returns 1 (which means the value in $Apples does match the pattern) or 0 (which means $Apples does not match the pattern) or FALSE (which means an error occurred).
Assuming 1 is returned, then $matches[0] will contain the entire portion of the $Apples string which matches the pattern. If you have capture groups then the portion of that match which falls within the first capture group will be found in $matches[1], second in $matches[2] and so on.
If you can't share your regex pattern it's not possible to see whether your pattern contains any capture groups, so let's use this example:
preg_match("/key:([A-Z]+);value:([0-9]+)/", "key:ERRORCODE;value:500", $matches);
Now $matches[0] should contain "key:ERRORCODE;value:500" because the entire string matches the pattern, and $matches[1] should contain "ERRORCODE", and $matches[2] should contain "500", because these portions fit the patterns in the capture groups of the full pattern.
This regex finds the right string, but only returns the first result. How do I make it search the rest of the text?
$text =",415.2109,520.33970,495.274100,482.3238,741.5634
655.3444,488.29980,741.5634";
preg_match("/[^,]+[\d+][.?][\d+]*/",$text,$data);
echo $data;
Follow up:
I'm pushing the initial expectations of this script, and I'm at the point where I'm pulling out more verbose data. Wasted many hours with this...can anyone shed some light?
heres my string:
155.101.153.123:simple:mass_mid:[479.0807,99.011, 100.876],mass_tol:[30],mass_mode: [1],adducts:[M+CH3OH+H],
130.216.138.250:simple:mass_mid:[290.13465,222.34566],mass_tol:[30],mass_mode:[1],adducts:[M+Na],
and heres my regex:
"/mass_mid:[((?:\d+)(?:.)(?:\d+)(?:,)*)/"
I'm really banging my head on this one! Can someone tell me how to exclude the line mass_mid:[ from the results, and keep the comma seperated values?
Use preg_match_all rather than preg_match
From the PHP Manual:
(`preg_match_all`) searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags.
After the first match is found, the subsequent searches are continued on from end of the last match.
http://php.net/manual/en/function.preg-match-all.php
Don't use a regex. Use split to split apart your inputs on the commas.
Regexes are not a magic wand you wave at every problem that happens to involve strings.
Description
To extract a list of numeric values which may include a single decimal point, then you could use this regex
\d*\.?\d+
PHP Code Example:
<?php
$sourcestring=",415.2109,520.33970,495.274100,482.3238,741.5634
655.3444,488.29980,741.5634";
preg_match_all('/\d*\.?\d+/im',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
yields matches
$matches Array:
(
[0] => Array
(
[0] => 415.2109
[1] => 520.33970
[2] => 495.274100
[3] => 482.3238
[4] => 741.5634
[5] => 655.3444
[6] => 488.29980
[7] => 741.5634
)
)