For a school project I need to find the position of the string "AAAAAA" in a long string. I need to use a for
loop. So far I came up with the following code:
<?php
$string1 = "TATAGTTTCCTCTCTATAT";
$string2 = str_repeat("AAAGCCTCAAATCTCTCTAGTAAAAAAGCCTCAAATCTCTCTAGTAAA", 6);
$count = strlen($string1.=$string2);
for($i = 0; $i < $count; $i++){
$string_to_find = $count{$i};
print(strpos($string_to_find, 'AAAAAA'));
}
?>
I can't get it to work. What am I doing wrong?
If you are using strpos(), you dont required to use for loop. you can get the results without that.
<?php
$string1 = "TATAGTTTCCTCTCTATAT";
$string_to_find="";
$string2 = str_repeat("AAAGCCTCAAATCTCTCTAGTAAAAAAGCCTCAAATCTCTCTAGTAAA", 6);
$count = strlen($string1.=$string2);
for($i = 0; $i < $count; $i++){
$string_to_find.=$string1{$i};
print(strpos($string_to_find, 'AAAAAA'));
}
?>
here is the code you can try.
I think doing it with a for
/ foreach
loop is really bad,
anyway, here is your code, changed your code a little, hope it works
<?php
$string1 = "TATAGTTTCCTCTCTATAT";
$string2 = str_repeat( "AAAGCCTCAAATCTCTCTAGTAAAAAAGCCTCAAATCTCTCTAGTAAA", 6 );
$count = strlen( $string1 .= $string2 );
$temp = null;
foreach( str_split( $string1 ) as $char ) {
$temp .= $char;
if ( ( $pos = strpos( $temp, "AAAAAA" ) ) !== false ) {
print( $pos." " );
$temp = null;
}
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.