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;
}
}
?>
Related
I'm new for this, can anyone solve my problem?
this is my code :
<?php
$data = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$datastart = 3;
$string = join(',', $data);
echo $string;
?>
This echo will be result :
Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
I want a result like this:
Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday
thank you for the answer :)
You can use array_slice to extract the portions of your array in the order you want to output them, and then array_merge to put them back together:
$data = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$datastart = 3;
$string = implode(',', array_merge(array_slice($data, $datastart), array_slice($data, 0, $datastart)));
echo $string;
Or you can use a simple for loop:
$len = count($data);
$string = '';
for ($i = 0; $i < $len; $i++) {
$string .= ($i > 0 ? ',' : '') . $data[($i + $datastart) % $len];
}
echo $string;
Output:
Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday
Demo on 3v4l.org
This is my sample string (this one has five words; in practice, there may be more):
$str = "I want to filter it";
Output that I want:
$output[1] = array("I","want","to","filter","it");
$output[2] = array("I want","want to","to filter","filter it");
$output[3] = array("I want to","want to filter","to filter it");
$output[4] = array("I want to filter","want to filter it");
$output[5] = array("I want to filter it");
What I am trying:
$text = trim($str);
$text_exp = explode(' ',$str);
$len = count($text_exp);
$output[$len][] = $text; // last element
$output[1] = $text_exp; // first element
This gives me the first and the last arrays. How can I get all the middle arrays?
more generic solution that works with any length word:
$output = array();
$terms = explode(' ',$str);
for ($i = 1; $i <= count($terms); $i++ )
{
$round_output = array();
for ($j = 0; $j <= count($terms) - $i; $j++)
{
$round_output[] = implode(" ", array_slice($terms, $j, $i));
}
$output[] = $round_output;
}
You can do that easily with regular expressions that give you the most flexibility. See below for the way that supports dynamic string length and multiple white characters between words and also does only one loop which should make it more efficient for long strings..
<?php
$str = "I want to filter it";
$count = count(preg_split("/\s+/", $str));
$results = [];
for($i = 1; $i <= $count; ++$i) {
$expr = '/(?=((^|\s+)(' . implode('\s+', array_fill(0, $i, '[^\s]+')) . ')($|\s+)))/';
preg_match_all($expr, $str, $matches);
$results[$i] = $matches[3];
}
print_r($results);
You can use a single for loop and if conditions to do
$str = "I want to filter it";
$text = trim($str);
$text_exp = explode(' ',$str);
$len = count($text_exp);
$output1=$text_exp;
$output2=array();
$output3=array();
$output4=array();
$output5=array();
for($i=0;$i<count($text_exp);$i++)
{
if($i+1<count($text_exp) && $text_exp[$i+1]!='')
{
$output2[]=$text_exp[$i].' '.$text_exp[$i+1];
}
if($i+2<count($text_exp) && $text_exp[$i+2]!='')
{
$output3[]=$text_exp[$i].' '.$text_exp[$i+1].' '.$text_exp[$i+2];
}
if($i+3<count($text_exp) && $text_exp[$i+3]!='')
{
$output4[]=$text_exp[$i].' '.$text_exp[$i+1].' '.$text_exp[$i+2].' '.$text_exp[$i+3];
}
if($i+4<count($text_exp) && $text_exp[$i+4]!='')
{
$output5[]=$text_exp[$i].' '.$text_exp[$i+1].' '.$text_exp[$i+2].' '.$text_exp[$i+3].' '.$text_exp[$i+4];
}
}
I have a string:
$content = "test,something,other,things,data,example";
I want to create an array where the first item is the key and the second one the value.
It should look like this:
Array
(
[test] => something
[other] => things
[data] => example
)
How can I do that? It's difficult to search for a solution because I don't know how to search this.
It's very similar to this: Explode string into array with key and value
But I don't have a json array.
I tried something like that:
$content = "test,something,other,things,data,example";
$arr = explode(',', $content);
$counter = 1;
$result = array();
foreach($arr as $item) {
if($counter % 2 == 0) {
$result[$temp] = $item;
unset($temp);
$counter++;
} else {
$temp = $item;
$counter++;
continue;
}
}
print_r($result);
But it's a dirty solution. Is there any better way?
Try this:
$array = explode(',',$content);
$size = count($array);
for($i=0; $i<$size; $i++)
$result[$array[$i]] = $array[++$i];
Try this:
$content = "test,something,other,things,data,example";
$data = explode(",", $content);// Split the string into an array
$result = Array();
$size = count($data); // Calculate the size once for later use
if($size%2 == 0)// check if we have even number of items(we have pairs)
for($i = 0; $i<$size;$i=$i+2){// Use calculated size here, because value is evaluated on every iteration
$result[$data[$i]] = $data[$i+1];
}
var_dump($result);
Try this
$content = "test,something,other,things,data,example";
$firstArray = explode(',',$content);
print_r($firstArray);
$final = array();
for($i=0; $i<count($firstArray); $i++)
{
if($i % 2 == 0)
{
$final[$firstArray[$i]] = $firstArray[$i+1];
}
}
print_r($final);
$content = "test,something,other,things,data,example";
$x = explode(',', $content);
$z = array();
for ($i=0 ; $i<count($x); $i+=2){
$res[$x[$i]] = $x[$i+1];
$z=array_merge($z,$res);
}
print_r($z);
I have tried this example this is working file.
Code:-
<?php
$string = "test,something|other,things|data,example";
$finalArray = array();
$asArr = explode( '|', $string );
foreach( $asArr as $val ){
$tmp = explode( ',', $val );
$finalArray[ $tmp[0] ] = $tmp[1];
}
echo "After Sorting".'<pre>';
print_r( $finalArray );
echo '</pre>';
?>
Output:-
Array
(
[test] => something
[other] => things
[data] => example
)
For your reference check this Click Here
Hope this helps.
You could able to use the following:
$key_pair = array();
$arr = explode(',', $content);
$arr_length = count($arr);
if($arr_length%2 == 0)
{
for($i = 0; $i < $arr_length; $i = $i+2)
{
$key_pair[$arr[$i]] = $arr[$i+1];
}
}
print_r($key_pair);
$content = "test,something,other,things,data,example";
$contentArray = explode(',',$content);
for($i=0; $i<count($contentArray); $i++){
$contentResult[$contentArray[$i]] = $contentArray[++$i];
}
print_r( $contentResult);
Output
Array
(
[test] => something
[other] => things
[data] => example
)
$contentResult[$contentArray[1]] = $contentArray[2];
$contentResult[$contentArray[3]] = $contentArray[4];
$contentResult[$contentArray[5]] = $contentArray[6];
I have a string that that is an unknown length and characters.
I'd like to be able to truncate the string after x amount of characters.
For example from:
$string = "Hello# m#y name # is Ala#n Colem#n"
$character = "#"
$x = 4
I'd like to return:
"Hello# m#y name # is Ala#"
Hope I'm not over complicating things here!
Many thanks
I'd suggest explode-ing the string on #, then getting the 1st 4 elements in that array.
$string = "Hello# m#y name # is Ala#n Colem#n";
$character = "#";
$x = 4;
$split = explode($character, $string);
$split = array_slice($split, 0, $x);
$newString = implode($character, $split).'#';
function posncut( $input, $delim, $x ) {
$p = 0;
for( $i = 0; $i < $x; ++ $i ) {
$p = strpos( $input, $delim, $p );
if( $p === false ) {
return "";
}
++ $p;
}
return substr( $input, 0, $p );
}
echo posncut( $string, $character, $x );
It finds each delimiter in turn (strpos) and stops after the one you're looking for. If it runs out of text first (strpos returns false), it gives an empty string.
Update: here's a benchmark I made which compares this method against explode: http://codepad.org/rxTt79PC. Seems that explode (when used with array_pop instead of array_slice) is faster.
Something along these lines:
$str_length = strlen($string)
$character = "#"
$target_count = 4
$count = 0;
for ($i = 0 ; $i<$str_length ; $i++){
if ($string[$i] == $character) {
$count++
if($count == $target_count) break;
}
}
$result = sub_str($string,0,$i)
I would like to return the longest string in an array in php 4.0 my sample code looks like this.
$MyArray=Array("Jane","Magdalene","Bull fighting champion","cruising","Tommy Lee Jones","View","axe");
$largest = max($MyArray);
echo $largest.
$longest = $MyArray[0];
foreach( $MyArray as $str ) {
if ( strlen( $str ) > strlen( $longest ) ) {
$longest = $str;
}
}
max() is to give the maximum from a list of integers. Unfortunately, your problem is more complicated.
<?php
$MyArray=Array("Jane","Magdalene","Bull fighting champion","cruising","Tommy Lee Jones","View","axe");
$maxlen = 0;
$idx = -1;
for ($i=count($MyArray); $i; $i--) {
$len = strlen($MyArray[$i-1]);
if ($len > $maxlen) {
$maxlen = $len;
$idx = $i-1;
};
}
if ($idx >0) {
echo $MyArray[$idx];
}
You can try this:
$lengths = array();
for($i=0; $i<count($MyArray); $i++) {
$lengths[$myArray[$i]] = strlen($MyArray[$i]);
}
arsort($lengths);
echo key($lengths);
And the compact version without manual loops:
$array = array_combine($array, array_map("strlen", $array));
arsort($array);
print key($array);