1

I need a regular expression what would search at the beginning of my string if there is a number followed a percent sign and extract it.

$string = "20% - some text";

preg_match('/^[0-9]+%$/',$string);

should return 20%

1 Answer 1

2

You should use the $matches argument:

$string = "20% - some text";
$matches = array();
if (preg_match('/^([0-9]+%)/', $string, $matches)) {
     print_r($matches);
}
Sign up to request clarification or add additional context in comments.

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.