PHP string thingy

Soldato
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
I have a list of letters and nulls, e.g. "ABCDEF----ACDFG-GEDA--CADSFA"

The - are important, and I'd like a way to process these and, for each "-" group in the string, return a list of the start number and width.

E.g., for the above, this would be:

7,4
15,1
21,2

I.e., there is a 4 - long group at charcter 7 etc...

I have the following to process string character by character but am stuck from then on. I did to it itereatively, i.e. is the next character a -, is there next character a - etc.. but this crashed Apache.

PHP:
$length = strlen(trim($seqrow['sequence'])); 
		$count = 0;
		$str=trim($seqrow['sequence']);
		while ($count < $length){
			if ($str[$count] == '-'){
		//process string character by character
					
			$count = $count +1;
		}
}

Any tips would be appreciated.

EDIT - to clarify, I guess what I want to right is something that will read each character, then if that character is a -, count the number of - until the next nondash character, and record the start pos/width of that frist group, then continue reading to find more groups... Clear as mud! :(
 
Last edited:
Your exit condition for the while loop can never be met unless the string comprises of all '-'. The 'if' statement will only increment if a '-' is encountered so the count will never equal the length unless the string is something like '--------'. You need something like a for loop.
 
Darnit. I created a new problem:

PHP:
<?
$ssv =  "---MDFKRLTAFFAIALVIMIGWEKMFPTPKPVP----------------APQQAAQQQAVTASAEAALA-------------------------------PATPITVTTDTVQAVIDEKSGDLRRLTLLKYKATGDENKP-FILFGDGK-EYTYVAQSELLDAQGNNILKG---IGFSAPKKQYSL-EGDK-VEVRLSAPETRGLKIDKVYTFTKGSYLVNVRFDIANGSGQTANLSADYRIVRDHSEPE-----GQGYFTHSYVGPVVYTPEGNFQKVSFSDLDDDAKSGKSEAEYIRKTPTGWLGMIEHHFMSTWILQPKGRQSVCAAGECNIDIKRRNDKLYSTSVSVPLAAIQNGAKAEASINLYAGPQTTSVIANI-AD-----NLQLAKDYGKVHW---FASPLFWLLNQLHNIIGNWGWAIIVLTIIVKAVLYPLTNASYRSMAKMRAAAPKLQAIKEKYGDDRMAQQQAMMQLYTDEKINPLGGCLPMLLQIPVFIGLYWALFASVELRQAPWLGWITDLSRADPYYIL-------------------PIIMAATMFAQTYLNPPP--TDPMQAKMMKIMPLVFSVMFFFFPAGLVLYWVVNNLLTIAQQWHINRS-IEKQRAQGEVVS-----------*";

$ssva = "PLFWLLNQLHNIIGNWG";
	
	preg_match_all("/-+/", $ssv, $matches, 0, (strpos(trim($ssv), $ssva))); //count hypens on line
	
	echo strlen(implode("", $matches[0]));

?>

I basically want to count all the - before the "PLFWLLNQLHNIIGNWG" sequence in the main string. I did use substr but this apparently can't count concurrent strings.

The answer should be 71, the above gives it as 33.

Any ideas?
 
Code:
$before = strstr($haystack, $needle, true);
$arr = str_split($before);
$hyphens = '';
foreach ($arr as $char) {
  if ($char == '-') $hyphens .= $char;
}
strlen($hyphens); // number of hyphens
 
Back
Top Bottom