0

Possible Duplicate:
How can I convert ereg expressions to preg in PHP?

So I have some strings of this type:

choose_from_library_something
choose_from_library_something2
choose_from_library_something3
...

And I need to search for choose_from_library_*

here is my regular expression that doesn't work:

}elseif (preg_match('choose_from_library_.*',$form_name)) {

What I'm doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Uffo
  • 9,628
  • 24
  • 90
  • 154

2 Answers2

4

You need to add delimiters to your regex:

preg_match('/^choose_from_library_.*/',$form_name)

EDIT: Added an anchor ^ to the beginning of the regex, to avoid matching don't_choose_from_library_, etc.

skunkfrukt
  • 1,550
  • 1
  • 13
  • 22
0

You may be better off using explode and count that off instead in your situation.

else if ( count ( explode("_" , $form_name) ) == 4 ) {

}

To answer your question, you need to have slashes around your pattern

/pattern/

also, asterisk means none or more, so even if you had no text, you it would still match it. The same would hold true for explode (but you can see if there was an empty string in the last element and return that as false).

Duniyadnd
  • 4,013
  • 1
  • 22
  • 29