Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm struggling with this..

I have a string like this.

$test2 = "AB:001,003;AC:001,003,004,005,008;AD:002,003,004,007,008,009";

It's two letters : 3 digits, (this can repeat) ;

This string will have multiple 2 letter prefixes, and the amount of 3 digit entries will vary.

I need to be able to find a match between a 2 letter prefix and an associated 3 digits number.

EG: Does AD003 exist in the string ? I know 003 appears mulitple times but I'd want to find just AD003, or another match I query for.

I thought converting the string to individual arrays named after the 2 letter prefixes would be the way to go..

$AB = array('001','003');
$AC = array('001','003','004',008);
$AD = array('002','003','004','007','008','009');

I've got as far as exploding it on the ; and then the :

$parts =  explode(";",$test2) ;
foreach ($parts as $value) {
    $a[] = explode(":", $value);
}
print_r ( $a );            

This results in :

Array
(
    [0] => Array
        (
            [0] => AB
            [1] => 001,002,003
        )

    [1] => Array
        (
            [0] => AC
            [1] => 001,003,004,005
        )

    [2] => Array
        (
            [0] => AD
            [1] => 002,003,004
        )

)

But I don't know how to take it further than that.

I want to be able to search the arrays for specific matches:

if (in_array("003", $AD)) { 
    echo "Match";
}

If there is an easier way than converting to array to find AD003 in the string, then I'm happy to try that.

Thanks

share|improve this question

5 Answers 5

up vote 3 down vote accepted

Or simply you can use preg_match to check in string

$string = "AB:001,003;AC:001,003,004,005,008;AD:002,003,004,007,008,009";

if( preg_match('/AD:([0-9,]+)?003/',$string)){
//Your code here
}
share|improve this answer
1  
this seems the best solution for the query you want to search for –  Abhishek Madhani Oct 14 '14 at 10:08
    
Thanks this seems the simplest solution. is there any problem doing this if( ! preg_match('/AD:([0-9,]+)?003/',$string)){ ? Thanks –  Rocket Oct 14 '14 at 10:50
    
@Rocket Definitely no –  senK Oct 14 '14 at 11:07
    
? No there is no issue ? or no don't do it ? :) Thanks –  Rocket Oct 14 '14 at 12:52

You can try this

<?php
// Example 1
$apple = "apple1 apple2 apple3 apple4 apple5 apple6";
$apples = explode(" ", $apple);
echo $apples[0]; // apple1
echo $apples[1]; // apple2...........

?>
share|improve this answer

try this

<?php

$test2 = "AB:001,003;AC:001,003,004,005,008;AD:002,003,004,007,008,009";

$final = array();
$parts =  explode(";",$test2) ;
foreach ($parts as $value) {

    $a = explode(":", $value);
    foreach(explode(",", $a[1])as $val)
    {
       $final[$a[0].$val] =  $val;     
    }

}

echo "<pre>";
print_r ($final);

Result will be

Array
(
    [AB001] => 001
    [AB003] => 003
    [AC001] => 001
    [AC003] => 003
    [AC004] => 004
    [AC005] => 005
    [AC008] => 008
    [AD002] => 002
    [AD003] => 003
    [AD004] => 004
    [AD007] => 007
    [AD008] => 008
    [AD009] => 009
)

you can check if the value AB001 exists by using array_key_exists('AB001',$array);

share|improve this answer
1  
the other 2nd best solutions other than regex, than can be used for searching your query –  Abhishek Madhani Oct 14 '14 at 10:38
$test2 = "AB:001,003;AC:001,003,004,005,008;AD:002,003,004,007,008,009";

$parts = explode( ';', $test2 );
foreach( $parts as $value ) {
    list( $key, $digits ) = explode( ':', $value );
    $a[$key] = explode( ',', $digits );
}

print_r( $a );

if( !empty( $a['AB'] ) && in_array( '003', $a['AB'] ) ) {
    echo "Yep, it's there.";
}

Here's the result:

Array
(
    [AB] => Array
        (
            [0] => 001
            [1] => 003
        )

    [AC] => Array
        (
            [0] => 001
            [1] => 003
            [2] => 004
            [3] => 005
            [4] => 008
        )

    [AD] => Array
        (
            [0] => 002
            [1] => 003
            [2] => 004
            [3] => 007
            [4] => 008
            [5] => 009
        )

)
Yep, it's there.
share|improve this answer

Use the first array value as key:

$parts = explode(":", $value);
$key = array_shift($parts);
$a[$key] = $parts;

Then you can do:

if(isset($a['AD']) && in_array("003", $a['AD']))
  ...
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.