Jump to content

[SOLVED] Regular Expression in PHP using preg_split?


Recommended Posts

Possibly:

Raw Match Pattern:
(?<!-[a-z]{2})-(?![a-z]{4}-)

PHP Code Example:
<?php
$sourcestring="your source string";
$matches=preg_split('/(?<!-[a-z]{2})-(?![a-z]{4}-)/i',$sourcestring);
echo "<pre>".print_r($matches,true);
?>

$matches Array:
(
    [0] => EM
    [1] => SA0048
    [2] => SIG_CODE
    [3] => AA-ISTE
    [4] => 0033
)

 

Depends on how it matches your data.

All of you are really good , It works ,but could it be more generic for [3] & [4] element, I mean

let says that those elements are not a fix length example:

 

EM-SA0048-SIG_CODE-AAXXX-IS-0033 or

 

EM-SA0048-SIG_CODE-AAXX-ISTEXXX-0033 or

 

EM-SA0048-SIG_CODE-AAXXXXXX-ISTEXXXXXXXXXXX-0033

 

Could be it possible?

 

 

 

 

If that's how your string is going to always look like, you could still just use explode and then glue elements 3 and 4 back together...

 

I like that idea better, no regex required.  If the same number of parts exist as in your string example:

<pre>
<?php
$str='EM-SA0048-SIG_CODE-AA-ISTE-0033';
$arr=explode('-',$str);
$arr[3]=$arr[3].'-'.$arr[4];
$arr[4]=array_pop($arr);
echo print_r($arr,true);
?>

 

If the number of parts may vary different code will be required.

 

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.