0

I am new to PHP. I need help to open file and find a line within double quotes after known word(namespace) and store it in another variable. The line after the word may be after a space or may be in the next line. Here is my code.

$string = 'namespace 
    "urn:fsd:fsdf:fdsfs"';
if (preg_match('/namespace  *"([^"]+)"/', $string, $m)) {
echo $m[1];
}

This displays null.

Sample input:

$string = 'namespace 
    "urn:fsd:fsdf:fdsfs"';

Output should be:

urn:fsd:fsdf:fdsfs

I am not getting the value i.e. urn:fsd:fsdf:fdsfs. I get a null. If the input is in the same line as below :

$string = 'namespace  "urn:fsd:fsdf:fdsfs"';

I get output. But if it is in next line null is displayed.

Saral
  • 1,087
  • 1
  • 8
  • 18
Pavithra
  • 1
  • 2
  • Give us a sample input and desired output please – delboy1978uk Jun 08 '18 at 10:43
  • See https://stackoverflow.com/a/48236101/3832970. And a double quoted string literal is matched with `"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"` (use `s` modifier as well). – Wiktor Stribiżew Jun 08 '18 at 10:44
  • @Wiktor Thanks for the immediate reply.But this is not the same as the one refered.I need to find the line next to the word even if it is in the next line.Can you please elaborate the answer with my example.Sorry I am new to php regex. – Pavithra Jun 08 '18 at 11:49
  • See http://rextester.com/ZIM31423 - instead of a space, use `\s`. The rest is optional, is only to enhance your code, it is `\s` that fixes it. – Wiktor Stribiżew Jun 08 '18 at 11:51
  • Thanks a lot.It is solved. – Pavithra Jun 08 '18 at 12:09

0 Answers0