1

Excuse me, I am making a terrible mistake somewhere, but this is the situaton:

In php i have:

$ln = "A/RADIUS ADMITS VALUE 20";
$trg = "A/RADIUS";
$matches = array();
$zz = preg_match('#$trg\sADMITS\s(VALUE)\s([^\s]+)#',$ln,$matches);

I want to capture the word "VALUE" without quotes and the last word, here the string 20, given by ([^\s]+) . That is not a whitespace repeated more than once right?

But $zz is 0, indicating no match and $matches is empty. I also tried with

 $zz = preg_match('#'.$trg.'\sADMITS\s(VALUE)\s([^\s]+)#',$ln,$matches);

same problem.

Where is the mistake I am stupidly making?

1

1 Answer 1

1

You are using single quotes to embed the $trg variable in the string, which only works when using double quotes to enclose the string. This should work:

$zz = preg_match("#$trg\sADMITS\s(VALUE)\s([^\s]+)#",$ln,$matches);
Sign up to request clarification or add additional context in comments.

3 Comments

@h2ooooooo Odd, as I can confirm it working as expected. See: 3v4l.org/ETYd5
while using single quote then using the '.' to concat the variable is NOT working, embedding the variable in a double quoted string IS working. Thank you a lot.
Yet another quetion, about $zz = preg_match('#'.$trg.'\sADMITS\s(VALUE)\s([^\s]+)#', $ln , $matches); failing : is it a bug in PHP ? should I file a report?

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.