-3

I have a string something like this

$str="syx.ypzd [xys.ypd] yup";

I am trying to get the value inside [ ] .

I have a code something like this

preg_match('[xys.ypd]', $str, $Fdesc);
echo $Fdesc[1];

But this is not working

Please let me know where I am going wrong

Thanks & Regards

Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130
  • You need to put more effort into solving your problems. For now, [see this](http://php-regex.blogspot.com/search/label/Regular%20Expressions) – Alex M Apr 20 '15 at 11:59

3 Answers3

2

You're not using the right syntax :

/\[(.*)\]/

This one will match what is between [ and ].

  • / the start of the regex
  • \[ matches the [
  • (.*) matches any number of characters (the parenthesis "capture" this part so you will get it in the results)
  • \] matches the ]
  • / the end of the regex

The code :

preg_match('/\[(.*)\]/', $code, $Fdesc);
FrancoisBaveye
  • 1,902
  • 1
  • 18
  • 25
1

Here is some code that works

  $str="syx.ypzd [xys.ypd] yup";
  preg_match('[xys.ypd]', $str, $Fdesc);
  print_r($Fdesc);

Output:

Array ( [0] => xys.ypd )

Lloyd Moore
  • 3,117
  • 1
  • 32
  • 32
1

Try this one.

preg_match('\[(.*)\]', $code, $Fdesc);
echo $Fdesc[1];
ag0702
  • 381
  • 1
  • 6
  • 18