0

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

2
  • 2
    i would just use explode() Commented May 25, 2014 at 20:54
  • Explode is not pretty for whole thing , i guess. i used regexfor first action and then explode function parameters with explode @Dagon Commented May 25, 2014 at 21:11

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, this is what i currently use. preg_match("/function::(.*)::end/",$CurrentSotoun['content'],$calling_function); any problem that you see ?
Yes, the quantifier is greedy. Did you notice the ? in my suggested solution?
We have a winner here !! its really helped me out , its hours that i working on it and its solved NOW ! thanks alot !
1

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.

6 Comments

That's not going to work on the second example, which is the one he is having problems with.
Then it's \S insead of . I'd say. Didn't see that string... Edited.
that seems exactly what i want , but i just tested it , not working
@user2077916: Please see the edit. I tested it against the data you have provided and both examples work.
Sorry , i tested it again , works fine , but i prefer to use the selected answer. thank you for sharing.again :)
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.