Jump to content

[SOLVED] Regular Expression in PHP using preg_split?


fredroines

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.