0

I have a long string like: B1C1F4G6H4I7J1J8L5O6P2Q1R6T5U8V1Z5, and i need to explode it on pairs so in this case 1 - B1 2 - C1 and so on...

The reason why i need that is that each pair contains 1 information in database so i have to take each of that pair and make some cycle or something to MySQL search

I was thinking about convert that sting to array like this:

1=> B1
2=> C1
3=> F4

and then foreach that with something like

foreach ($array as $arr)
{
  //and here come database search
}

But maybe i am completly wrong and to say true everything i try finish with ERROR so guys i am up for any advise, if somebody have time to write an example code it will be awesome.

Thanks!

p.s. First of all i was thinking about something like:

$string[0] = substr($string,0,2);
$string[1] = substr($string,2,4);

but string will changing and I never know how long it will be

3
  • Hint: substr + for + strlen Commented Jan 16, 2015 at 8:19
  • 1
    $array = str_split($string, 2); Commented Jan 16, 2015 at 8:21
  • "but string will changing and I never know how long it will be" --- that's why you need strlen. Then just iterate from 0 up to strlen with + 2 step. Commented Jan 16, 2015 at 8:22

4 Answers 4

2

You can use str_split()

$str = 'B1C1F4G6H4I7J1J8L5O6P2Q1R6T5U8V1Z5';
$ar = str_split($str,2);
print_r($ar);

Make sure you access the first element with zero index.

Sign up to request clarification or add additional context in comments.

2 Comments

Actualy working and looks pretty easy :) Thumb up for that already. Is there a chance you can help me to write how can i PDO SQL statement for each of this pairs?
Glad to help! For PDO/SQL you should post another question, so people can find it separately :)
2

You can always access a string like an array in itself without having to do anything to it like this:

$string='B1C1F4G6H4I7J1J8L5O6P2Q1R6T5U8V1Z5';
for($i=0;$i<strlen($string)/2;$i++)
{
    $arr[]=$string[$i*2].$string[($i*2)+1];
}

print_r($arr);

Comments

1

Php function str_split might work

>>> $text = 'B1C1F4G6H4I7J1J8L5O6P2Q1R6T5U8V1Z5'
>>> str_split($text, 2)
=> [
       "B1",
       "C1",
       "F4",
       "G6",
       "H4",
       "I7",
       "J1",
       "J8",
       "L5",
       "O6",
       "P2",
       "Q1",
       "R6",
       "T5",
       "U8",
       "V1",
       "Z5"
   ]

2 Comments

This looks good, so now I just need to foreach mysql command or what you suggest?
then you will have to traverse the array and do whatever operations you will need, str_split returns the array you will need
0

You should use recursion:

$string = "B1C1F4G6H4I7J1J8L5O6P2Q1R6T5U8V1Z5";
$array = recursion($string);

print_r($array);



function recursion ($string, $array = array()){
    if (strlen($string) < 1){
        return $array;
        }else{
            $array[] = substr($string, 0 ,2);
            return recursion(substr($string, 2), $array);
        }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.