2

Not sure how I would do this but if someone could point me in the right track that'll be great, basically I've got a lone line of text in a variable which looks like this:

Lambo 1; Trabant 2; Car 3;

Then I want to split "Lambo" to it's own variable then "1" to it's own variable, and repeat for the others. How would I go and do this? I know about explode() but not sure how I would do it to split the variable up twice etc.

As requested in the comments my desired output would be like this: $Item = "Lambo" $Quantity = 1 Then echo them out and go back to top of loop for example and do the same for the Trabant and Car

4
  • Have you considered using PHP's explode() function? or preg_split()? Commented May 22, 2016 at 18:04
  • @MarkBaker Hey, I've looked at it but I'm not sure how I would split it up like above, I want for example "Lambo" in one variable e.g $Item then the number after it $Quantity then I'll echo them out, and repeat for the other two etc. Commented May 22, 2016 at 18:06
  • Add an example of your desired output .. Commented May 22, 2016 at 18:06
  • You explode once to an array splitting on the ;, then walk that resulting array splitting on the space Commented May 22, 2016 at 18:06

6 Answers 6

2
<?php
$in = "Lambo 1; Trabant 2; Car 3;";
foreach (explode(";", $in) as $element) {
        $element = trim($element);
        if (strpos($element, " ") !== false ) {
                list($car, $number) = explode(" ", $element);
                echo "car: $car, number: $number";
        }
}

You can use explode to split the input on each ;, loop over the results and then split over each .

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

2 Comments

Hey, any chance you could tell me how to put a MySQL Query into a format like $in? $result=mysqli_query($conn,$sql_query);
@Paul Please post your question as a new question, not as a comment to an existing question.
0

You can use preg_split and iterate over the array by moving twice.

$output = preg_split("/ (;|vs) /", $input);

Comments

0

You could use preg_match_all for getting those parts:

$line = "Lambo 1; Trabant 2; Car 3;";   

preg_match_all("/[^ ;]+/", $line, $matches);
$matches = $matches[0];

With that sample data, the $matches array will look like this:

Array ( "Lambo", "1", "Trabant", "2", "Car", "3" ) 

Comments

0
   $new_data="Lambo 1;Trabant 2;Car 3;"  ;  
   $new_array=explode(";", $new_data);
      foreach ($new_array as $key ) {
          # code...
            $final_data=explode(" ", $key);

           if(isset($final_data[0])){ echo "<pre>".$final_data[0]."</pre>";}
           if(isset($final_data[1])){echo "<pre>".$final_data[1]."</pre>";}
    }

Comments

0

This places each word and number in a new key of the array if you need to acess them seperatly.

preg_match_all("/(\w+) (\d+);/", $input_lines, $output_array);

Click preg_match_all http://www.phpliveregex.com/p/fM8

Comments

0

Use a global regular expression match:

<?php
$subject = 'Lambo 1; Trabant 2; Car 3;';
$pattern = '/((\w+)\s+(\d+);\s?)+/Uu';
preg_match_all($pattern, $subject, $tokens);
var_dump($tokens);

The output you get is:

array(4) {
  [0] =>
  array(3) {
    [0] =>
    string(8) "Lambo 1;"
    [1] =>
    string(10) "Trabant 2;"
    [2] =>
    string(6) "Car 3;"
  }
  [1] =>
  array(3) {
    [0] =>
    string(8) "Lambo 1;"
    [1] =>
    string(10) "Trabant 2;"
    [2] =>
    string(6) "Car 3;"
  }
  [2] =>
  array(3) {
    [0] =>
    string(5) "Lambo"
    [1] =>
    string(7) "Trabant"
    [2] =>
    string(3) "Car"
  }
  [3] =>
  array(3) {
    [0] =>
    string(1) "1"
    [1] =>
    string(1) "2"
    [2] =>
    string(1) "3"
  }
}

In there the elements 2 and 3 hold exactly the tokens you are looking for.

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.