0

for example I have this string. #345-6574#56-2432#776-246554#

I want to take all values between sharps.

I tried this but not working

preg_match_all('/^#[\d]+-[\d]+#$/',$string,$output);

2 Answers 2

2

A regex would be unnecessary:

$output = array_filter(explode('#', $string));

See it in action

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

2 Comments

thanks for answer which is better for performance. regex or explode ?
explode() should be faster. It also is also easier to understand which makes future maintenance easier.
1

This will do:

preg_match_all('/#(\d+\-\d+)#/',$string,$output);

You use lookahead and lookbehind as well if you want to make sure # to be at the both ends.

preg_match_all('/(?<=#)(\d+\-\d+)(?=#)/',$string,$output);

1 Comment

I need digits-digits in output array. not sharps. it returns values with sharps.

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.