0

i have this code:

$text = "###12###hello###43###good###113###thefinalstring";
preg_match_all('/(.*?)###(\d*)###(.*?)/is', $text, $matches, PREG_SET_ORDER);

If I dump $matches, why there is not "thefinalstring" anywhere? Where is the error in the regular expression?

Thanks

6
  • Can you explain what is your goal, what do you exactly want to match? Commented Jun 6, 2013 at 15:21
  • (.*?)###(\d*)###(.*?)([a-zA-Z]*)..Is it solving your purpose? Commented Jun 6, 2013 at 15:21
  • What are you trying to achieve ? are seeking for thefinalstring only ? Commented Jun 6, 2013 at 15:43
  • i want to know every part of the string, that is interrupted by a ###number### (and there can be any kind of chars) Commented Jun 6, 2013 at 19:17
  • so you want "hello", "good" and "thefinalstring" as matched result? Commented Jun 7, 2013 at 4:22

2 Answers 2

1
(.*?)###(\d*)###(.*?)([a-zA-Z]*)

Use this regex

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

1 Comment

mmm this work with only normal chars.. how can I say, all chars?
0

Have a try with:

$text = "###12###hello###43###good###113###thefinalstring";
preg_match_all('/###(\d*)###([^#]*)/is', $text, $matches, PREG_SET_ORDER);
print_r($matches);

output:

Array
(
    [0] => Array
        (
            [0] => ###12###hello
            [1] => 12
            [2] => hello
        )

    [1] => Array
        (
            [0] => ###43###good
            [1] => 43
            [2] => good
        )

    [2] => Array
        (
            [0] => ###113###thefinalstring
            [1] => 113
            [2] => thefinalstring
        )

)

3 Comments

we are close.. if it finds a #, it break... I want to break only if it finds another ###number###
@OscarFanelli: Sorry, I don't get you. You could show sample strings and what you want to match for each of them.
Sample 1: <p>Hello###405### </p><p>###32### </p> Sample 2: ###403### Hello Sample 3: Hello ###420001### Sample 4: <div>Bye</div>

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.