Jump to content

Recommended Posts

Hi guys, hope someone can help, Im having some problems spliting a string using the preg_split function. Basically im trying to split a string similar to this.

 

x > 1

Output needs to be array([0]=>x[1]=>>[2]=>1)

 

I need a regex that can also split on

 

x >= 1  or

x <= 1 or

x = 1 or

x != 1

 

Currently im using this, $regx = "/(<=|<|=|>|>=)/"; but for expressions like x >= 1  the returned array contains 2 elements one for each operator, I need just the one.

 

Any help will be greately appreciated.

Link to comment
https://forums.phpfreaks.com/topic/221135-help-with-regex/
Share on other sites

Thank you for your reply, I should have mentioned that the space in the expression may or may not be present.

You may have to trim some of the results:

 

$array = preg_split('/([<>=!]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE);

Link to comment
https://forums.phpfreaks.com/topic/221135-help-with-regex/#findComment-1145061
Share on other sites

Currently im using this, $regx = "/(<=|<|=|>|>=)/"; but for expressions like x >= 1  the returned array contains 2 elements one for each operator, I need just the one.

 

With that regex, >= will never get matched because > and = both occur individually before it in the group of alternatives.  Matching stops at the earliest successful match of an alternative.  If you moved >= earlier (at least, before = and >), then it would work fine just like <= does.

Link to comment
https://forums.phpfreaks.com/topic/221135-help-with-regex/#findComment-1145126
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.