PaulB3434
PaulB3434

Reputation: 27

PHP - Splitting string lines up into two variables

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

Upvotes: 2

Views: 63

Answers (6)

arkascha
arkascha

Reputation: 42885

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.

Upvotes: 0

Andreas
Andreas

Reputation: 23958

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

Upvotes: 0

Stephen Mudere
Stephen Mudere

Reputation: 454

   $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>";}
    }

Upvotes: 0

Johann Bauer
Johann Bauer

Reputation: 2606

<?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 .

Upvotes: 2

trincot
trincot

Reputation: 350137

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" ) 

Upvotes: 0

user3339197
user3339197

Reputation:

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

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

Upvotes: 0

Related Questions