0
if (stripos($jml['JENIS_TOKO'],'buah') !== false || stripos($jml['JENIS_TOKO'],'bakery') !== false || stripos($jml['JENIS_TOKO'],'roti') !== false) {

Is there any way to shorten this if condition in PHP? Just like WHERE JENIS_TOKO REGEXP 'buah|bakery|roti' in SQL.

It's really not a problem if it just 3 like in my example, but I know it won't be effective & the code becomes too long if my substrings is ten or more. (ps: I don't know about regex, but I'm sure that's the clue)

EDIT: This is not a possible duplicate as alleged. I already check reffered question and see the approved answer, but my case is different. I use bunch of strings, not an array, thus I don't want to use array & foreach loop unless that is the only way.

I already explain: "Just like WHERE JENIS_TOKO REGEXP 'buah|bakery|roti'" so I'm sure there's a way to solve this with a regular expression method in single line.

8
  • preg_match() would be shorter Commented Aug 31, 2017 at 1:44
  • @rtfm yeah I know that will, but I need the answer with example to solve my case, because I don't understand about regular expression yet. then I will learn from the answer. thank you. Commented Aug 31, 2017 at 1:48
  • manual page has great examples Commented Aug 31, 2017 at 1:51
  • 1
    if(preg_match("#\b(buah|bakery|roti)\b#",$jml['JENIS_TOKO'])){ ideone.com/XChMCJ Commented Aug 31, 2017 at 2:35
  • 1
    Try with: if(preg_match('#(buah|bakery|roti)#i', $jml['JENIS_TOKO']) === 1) { ... } Commented Aug 31, 2017 at 2:41

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.