1

I want to preg_match_all() this:

    if(isset($matches[1])){
        return $matches[1];
    }else{
        return false;
    }
}
$lines = file('jason.txt');
$i=0;
foreach ($lines as $line_num => $line) {
    $str_arr = getInbetweenStrings('"Vehicle":', ',"Buyer":', $line);
    echo '<pre>';
    print_r($str_arr);
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 1
    The string is `"Vehicle": "1992 Macho Camry CE"`? – Mohammad Feb 24 '17 at 12:25
  • show how do you invoke `getInbetweenStrings` function – RomanPerekhrest Feb 24 '17 at 12:27
  • It is in a array "QuoteRequestId": 343816132, "Vehicle": "1992 Macho Camry CE", "Buyer": "Carolina Auto Body", – Bhrigu Dash Feb 24 '17 at 12:28
  • If you start with json somewhere up the line, you'd better parse that instead. – jeroen Feb 24 '17 at 12:29
  • unclear, show more code with those `$start, $end, $str` arguments values – RomanPerekhrest Feb 24 '17 at 12:29
  • and what are the values? – RomanPerekhrest Feb 24 '17 at 12:36
  • @BhriguDash What is the status of your quesition? Did either of the answers below solve your issue? If so, please award one of them the green tick. If not, please update your question to explain what is not being satisfied by the current answers. – mickmackusa Mar 28 '17 at 15:44
  • @mickmackusa it does not worked i have parsed it through jquery – Bhrigu Dash Apr 13 '17 at 13:00
  • @BhriguDash It is unclear what you mean. Are you saying the current list of answers didn't help and you solved it yourself using jquery? Or are you still looking for a solution? If you are still looking to manipulate the data using `php` and `preg_match_all` please update your question to provide sample input data and the expected output using the input data. I will help you. – mickmackusa Apr 13 '17 at 21:00
  • @mickmackusa actually the json format is not correct so it is used to parse through jquery and then decoded by php – Bhrigu Dash Apr 21 '17 at 10:26
  • @BhriguDash Is your issue resolved? or is there still something to fix? Can you update your question with a sample string? If you have already solved it please provide an answer. All questions on SO should come to some sort of resolution. I will help if there is still something to do. – mickmackusa Apr 21 '17 at 11:05
  • @BhriguDash Can you please update or delete your question so that it is not an information deadend for SO readers? I would like to help if there is still something to fix. – mickmackusa Apr 27 '17 at 02:22
  • @mickmackusa i have solved it by parsing through getting the object through jquery. – Bhrigu Dash Jun 30 '17 at 13:00
  • @BhriguDash Then it seems your question will be forever doomed to be "abandoned / unanswered". When future readers come to this page looking for a php solution they will not know if either of the answers provided are functional/successful and readers won't know what you did to solve your issue. – mickmackusa Jun 30 '17 at 20:57

2 Answers2

0

If the string is:

"Vehicle": "1992 Macho Camry CE"

Here is the regex:

preg_match_all("/: \"?([\w ]+)/", $str, $matches,  PREG_PATTERN_ORDER);

Then call

print_r($matches);

Which will return:

Array
(
    [0] => Array
        (
            [0] => : "1992 Macho Camry CE
        )

    [1] => Array
        (
            [0] => 1992 Macho Camry CE
        )

)

To get the string, use:

$phrase = $matches[1];

edit: Because of the source data is a json string, use the json_decode function to convert all data in array:

$str = '[{"Vehicle": "1992 Macho Camry CE"}, {"Vehicle": "2017 OtherCar"}]';
$vehicles = json_decode($str, true);
print_r($vehicles);

Array
(
    [0] => Array
        (
            [Vehicle] => 1992 Macho Camry CE
        )

    [1] => Array
        (
            [Vehicle] => 2017 OtherCar
        )
)
user2342558
  • 5,567
  • 5
  • 33
  • 54
0

Based on your comment you are parsing a json file.

In that case you should not use regexes or string functions. Instead you should parse the json file directly.

To get it all in a multi-dimensional array structure:

$array = json_decode(file_get_contents('path/to/file.json'), true);
jeroen
  • 91,079
  • 21
  • 114
  • 132