0

I have a string that has been concatenated by the rows and columns that has be separated by using the special characters. Below is my code:

$strMaterialDetails = '13-"9"Strawberry%*DEALER%*15%*25%*375%*7.500%*7.500%*28.13%*2.000%*2.000%*6.94@^$14-"9" Yellow white Acrylic Long Pile Ext Roller Set%*DISTRIBUTOR%*45%*75%*3375%*2.500%*2.500%*84.38%*5.000%*5.000%*164.53@^$';

$pairs = explode('@^$', $strMaterialDetails);

foreach ($pairs as $pair) {
    list($product, $store, $qty, $unit, $total_price, $discount, $discount_percen, $discount_value, $tax, $tax_percen, $tax_val) = explode('%*', $pair);

    echo $store;
}

It works fine I have to insert the values into the database if I print the $store it will print DEALERDISTRIBUTOR

I want to seperate this I don't know how to do. The string here entered is unknown.

Please give any suggestion.

3
  • Please fix your code. The first line is invalid. Commented May 21, 2014 at 6:56
  • maybe some characters in the research pattern need to be escaped? Commented May 21, 2014 at 7:01
  • now check the code. i want to separate the rows and columns Commented May 21, 2014 at 7:09

2 Answers 2

1

Why not store it in an array?

$stores = array();
foreach ($pairs as $pair) {
    list($product,$store,$qty,$unit,$total_price,$discount,$discount_percen,$discount_value,$tax,$tax_percen,$tax_val) = explode('%*', $pair);    
    $stores[] = $store;
}

print_r($stores); // will contain DEALER at 0th index and DISTRIBUTOR at 1st index

DEMO

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

Comments

1

Your DEALERDISTRIBUTOR is not store in $store

actualy you are printing the $store in loop without any space that why it is printing combine.

originaly in first loop $store=DEALER then you echo it. and in second loop $store=DISTRIBUTOR so it is confustion only.

you can have Two solution for check it.

Solution : 1 print a new line after echo the $store like this

foreach ($pairs as $pair) 
{
       list($product,$store,$qty,$unit,$total_price,$discount,$discount_percen,$discount_value,$tax,$tax_percen,$tax_val) = explode('%*#', $pair);

        echo $store;
        echo "<br/>";

 }

Solution : 2 save $store in array

foreach ($pairs as $pair) 
    {
           list($product,$store,$qty,$unit,$total_price,$discount,$discount_percen,$discount_value,$tax,$tax_percen,$tax_val) = explode('%*#', $pair);

           $arr_store[] = $store;

     }
    print_r($arr_store);

1 Comment

the string is in unknown character i cant separate all the string using substr. i want to sepatare this as rows

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.