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.
Related
My string:
How would you rate the ease and comfort required to undertake the session?#QUESTION_VALUE_0
How would I be able to get this value specifically from the above string? I don't know what this value will be (apart from that it will be an integer):
(some question)#QUESTION_VALUE_X where X is an integer, I want to get X.
I looked into Regex, but I suck at regular expressions, so I'm at a loss, cheers guys!
About as far as I got with regex
/#QUESTION_VALUE_[0-9]+/
But I can't get the number out of the string. How can I only grab the number?
This should work for you:
Just put the escape sequence \d (which means 0-9) with the quantifier + (which means 1 or more times) into a group (()) to capture the the number which you then can access in the array $m.
<?php
$str = "How would you rate the ease and comfort required to undertake the session?#QUESTION_VALUE_0";
preg_match("/#QUESTION_VALUE_(\d+)/", $str, $m);
echo $m[1];
?>
output:
0
If you do print_r($m); you will see the structure of your array:
Array
(
[0] => #QUESTION_VALUE_0
[1] => 0
)
And now you see ^ that you have the full match in the first element and then first group ((\d+)) in the second element.
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 have been using a regex expression as follows. My aim is to extract a once decimal point number like 8.4 from a string. The code I have used is:
$reg = "/[0-9]+[ ]+([0-9]\.[0-9])/";
preg_match_all($reg, $buffer, $matches);
For an input like
0000001222 86257 8.4
I am getting the array $matches as:
Array
(
[0] => Array
(
[0] => 86257 8.4
)
[1] => Array
(
[0] => 8.4
)
)
Why is the pattern matched 2 times? I would like my matches array to be like:
Array
(
[0] => 8.4
)
Match #0 in a regular expression is (almost) always the entire matched string. In your case, because you're using parentheses (a capture group) you're telling the expression to capture a subset of the matched string too. Capture groups are returned starting from match #1.
If you explicitly don't want to capture the full string, consider using lookarounds instead; note that you'll need to refactor your expression a bit because lookarounds in PHP lookbehinds need to be zero-width, and thus quantifiers (the +) are not allowed.
You should check the manual (PREG_PATTERN_ORDER is the default):
Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on.
So in your case the result you want will be always in $matches[1].
I am trying to parse a integer from a list of uris like this:
uri.com/upload/123456789_abc.ext
I am using this pattern:
preg_match( "#uri\.com\/upload\/(.*?)_#is", $uri, $match );
Which works and returns:
Array
(
[0] => uri.com/upload/123456789_
[1] => 123456789
)
But I was wondering if there's a way to make $match == "123456789" intead of returning an array with multiple values.
Is it possible to do it by only modifying the pattern?
It will always return an array, but you can change the pattern, so that it only matches what you want.
$uri = "uri.com/upload/123456789_abc.ext";
preg_match('#(?<=uri\.com/upload/)\d+#is', $uri, $match );
print_r($match);
returns
Array ( [0] => 123456789 )
so it is still an array, but it does only contain the whole match, that is your number.
(?<=uri\\.com/upload/) is a lookbehind, it does not match that part, so it is not part of the result.
\d+ is only matching digits, so the _ is not needed anymore.
Not in php. In perl, on the other hand, you do have automatic variables, like $1, $2 that refer to the last matching regex. That is, in your example, after performing the match, $1 variable would hold the integer.
So the idea is generally ok. You would love perl.. :-)
I am using preg_match_all to search for HashTag values in a Twitter Search response.
It works as I expected except for when the search results don't have any hash values in them. For some reason my $tags array still has values and I'm not sure why.
Is it because my RegEx is not correct, or is it a problem with preg_match_all?
Thanks
$tweet = "Microsoft Pivot got Runner-Up for Network Tech from The Wall Street Journal in 2010 Technology Innovation Awards http://bit.ly/9pCbTh";
private function getHashTags($tweet){
$tags = array();
preg_match_all("/(#\w+)/", $tweet, $tags);
return $tags;
}
results in:
Array ( [0] => Array ( ) [1] => Array ( ) )
Expected results:
Array();
In default mode, preg_match_all returns an array of matches and submatches:
PREG_PATTERN_ORDER
Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on.
So in this case the first array is the array of matches of the whole pattern and the second array is the array of matches of the first subpattern. And since there was no match found, both arrays are empty.
If you want the other order, having each match in an array with its submatches, use PREG_SET_ORDER in the flags parameter:
preg_match_all("/(#\w+)/", $tweet, $tags, PREG_SET_ORDER);
You get two empty arrays because you are matching an expression and a subexpression. Your expected results are actually the error here. Check the manual, specifically the description of the default behavior when no flags are passed in the fourth argument:
Orders results so that $matches[0] is an array of full pattern matches, $matches1 is an array of strings matched by the first parenthesized subpattern, and so on.
You always get a multi-dimensional array from preg_match_all unless you pass PREG_OFFSET_CAPTURE as the flag argument. In this case, you should actually get an empty array for an expression that doesn't match anything.