php - splitting a string up

Status
Not open for further replies.
I've got data that comes in the following format, and I have no control over the format:

UPPERCASEATHROUGHZTEXTdata that does not contain any a-z alphabet charactersUPPERCASEATHROUGHZTEXTdata that does not contain any a-z alphabet charactersUPPERCASEATHROUGHZTEXTdata that does not contain any a-z alphabet charactersUPPERCASEATHROUGHZTEXTdata that does not contain any a-z alphabet characters

I need to split it up into an array where each element contains the following:

UPPERCASEATHROUGHZTEXTdata that does not contain any a-z alphabet characters
UPPERCASEATHROUGHZTEXTdata that does not contain any a-z alphabet characters
UPPERCASEATHROUGHZTEXTdata that does not contain any a-z alphabet characters
UPPERCASEATHROUGHZTEXTdata that does not contain any a-z alphabet characters

There are no : in the string anywhere, it's not a legal character for the data, so why couldn't they use it as a delimeter? Ah well.

The string can be quite large as in sometime 500K to a million characters, though usually it is smaller. I know preg_replace doesn't work well in those situations with such large strings, so adding a : delimiter via preg_replace so I could just explode it would work some of the time but not all the time.

Any ideas?
 

KrispyK

Ars Scholae Palatinae
911
if you don't want to use a regular expression, and you can't use an explode, then you'll just have to walk the entire string character by character, something like

Code:
/* WARNING, untested code, here be dragons! */

$strLen       = strlen($str); // do this outside of the for loop (for speed)
$posUppercase = null;
$otherData    = false;
$arrResult    = array();

for ($x=0;$x<=$strLen;$x++) { 
  
  if (ord($str[$x]) >= 65 && ord($str[$x]) <= 90) {
    // is uppercase character
    if ($otherData == true) { 
      // store position of the uppercase character
      $posUppercase = $x;
    }
  } else { 
    $otherData = true; 
  }
  if ($otherData == true && $posUppercase !=null ) { 
    $arrResult[] = substr($str,$startPos,($posUppercase-1) );
    $startPos = $x;
    $otherData = false; $posUppercase = null;
  }
}
print_r($arrResult);
 

Tornadus

Ars Scholae Palatinae
1,214
Is that string already read into a variable, or is it stored in a file?

If already a variable, this should do it:

Code:
$string = preg_replace( '/[A-Z]{26}/', ':', $string );

What will happen is that any text with the letters A through Z (regardless of order) that occur in a sequence of 26 characters will be replace (as a whole) with a single colon.

Alternatively, you can use this as well:

Code:
$string = preg_replace( '/ABCDEFGHIJKLMNOPQRSTUVWXYZ/', ':', $string );

Which does the same thing, but the order in which the letters occur actually matters.
 

Alamout

Ars Legatus Legionis
27,285
Tornadus":20jhs14o said:
Is that string already read into a variable, or is it stored in a file?

If already a variable, this should do it:

Code:
$string = preg_replace( '/[A-Z]{26}/', ':', $string );

What will happen is that any text with the letters A through Z (regardless of order) that occur in a sequence of 26 characters will be replace (as a whole) with a single colon.
This isn't what the OP actually wants to do at all...he doesn't want to get rid of the A-Z portion, and it isn't the literal alphabet (not necessarily 26 letters). Not to mention that he specifically doesn't want to use preg_replace...
 

Tornadus

Ars Scholae Palatinae
1,214
Alamout":1k33lx9i said:
Tornadus":1k33lx9i said:
Is that string already read into a variable, or is it stored in a file?

If already a variable, this should do it:

Code:
$string = preg_replace( '/[A-Z]{26}/', ':', $string );

What will happen is that any text with the letters A through Z (regardless of order) that occur in a sequence of 26 characters will be replace (as a whole) with a single colon.
This isn't what the OP actually wants to do at all...he doesn't want to get rid of the A-Z portion, and it isn't the literal alphabet (not necessarily 26 letters). Not to mention that he specifically doesn't want to use preg_replace...

Oh wow, I completely misread the OP.

This is what I get for problem solving before my coffee...

My bad.
 
(I'm just grasping for ideas)

try some sort of ascii check to find out if the character is uppercase or not? it only returns the first character but you could always remove the first character each time in a loop and save to a new row in an array when you hit an uppercase after a lowercase

perhaps look at ord()

otherwise, I'd get the data is a less crappy format.
 
Why not use strspn?

Code:
$splitPt = strspn( $string, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' );
$header = substr( $string, 0, $splitPt );
$data = substr( $string, $splitPt );

According to a quick benchmark the strspn is about 2 orders of mangitude faster than using a for loop.
Code:
String Length: 1000000

Using builtin strspn.
Position:127009
Substr (last 10 chars of head section): IRENIBURZR
Substr (first 10 chars of data): 2304343406
Time: 0.0044801235198975

Using For Loop.
Position: 127009
Substr (last 10 chars of head section): IRENIBURZR
Substr (first 10 chars of data): 2304343406
Time: 0.2495379447937

Edit: IME always dig though the PHP docs, especially for string/text manipulation stuff. AFAIK the built in functions are native code not PHP running on the Zend engine and well almost always be faster than trying to do something with a loop especially if you can cut down from several 1000 function calls to 1.
 
Status
Not open for further replies.