Jump to content

Problem selecting text with preg_match


Alhazred

Recommended Posts

I've got a several strings like these:

Firstname1 Lastname1 (13) reported contact (0.06) with another vehicle Firstname2 Lastname2 (37)

Firstname3 Lastname3 (45) reported contact (0.03) with another vehicle Firstname4 Lastname4 (54)

Firstname5 Lastname5 (42) reported contact (0.42) with another vehicle Firstname6 Lastname6 (22)

 

What I need to do is to create an array like this:

array(
    array(
        Name => "Firstname1 Lastname1", 
        Type => "reported contact with another vehicle Firstname2 Lastname2",
        Intensity => 0.06 
    ),
    array(
        Name => "Firstname3 Lastname3", 
        Type => "reported contact with another vehicle Firstname4 Lastname4",
        Intensity => 0.03
    ),
    array(
        Name => "Firstname5 Lastname5", 
        Type => "reported contact with another vehicle Firstname6 Lastname6",
        Intensity => 0.42
    )
);

I'm taking the first names in this way inside a foreach loop (strings come from an XML file)

preg_match('/(.*?)\(/', $incident, $name);

 

but with "echo $name[0]" I get "Firstname1 Lastname1 ("

so I also get the ( which I don't want, but I have use it in the regex pattern as a marker

 

The same thing happen when I use

preg_match('/\)(.*?)\(/', $incident, $type);

to take the first part of the incident description

echo $type[0] prints ") reported contact ("

and I don't want the ) and (

 

How do I have to do to get only the parts which I need?

Link to comment
https://forums.phpfreaks.com/topic/219606-problem-selecting-text-with-preg_match/
Share on other sites

<?php

// example data set
$data[] = "Firstname1 Lastname1 (13) reported contact (0.06) with another vehicle Firstname2 Lastname2 (37)";
$data[] = "Firstname3 Lastname3 (45) reported contact (0.03) with another vehicle Firstname4 Lastname4 (54)";
$data[] = "Firstname5 Lastname5 (42) reported contact (0.42) with another vehicle Firstname6 Lastname6 (22)";

// loop
foreach ($data as $key => $string) {
  $string = preg_split('~\(([^)]+)\)~',$string,-1,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
  $array[$key]['Name'] = trim($string[0]);
  $array[$key]['Type'] = trim($string[2]) . ' ' . trim($string[4]);
  $array[$key]['Intensity'] = trim($string[3]);
}

// final multi-dim array
echo "<pre>";
print_r($array);

?>

 

output:

 

Array
(
    [0] => Array
        (
            [Name] => Firstname1 Lastname1
            [Type] => reported contact with another vehicle Firstname2 Lastname2
            [intensity] => 0.06
        )

    [1] => Array
        (
            [Name] => Firstname3 Lastname3
            [Type] => reported contact with another vehicle Firstname4 Lastname4
            [intensity] => 0.03
        )

    [2] => Array
        (
            [Name] => Firstname5 Lastname5
            [Type] => reported contact with another vehicle Firstname6 Lastname6
            [intensity] => 0.42
        )

)

but to specifically answer your question, the reason you are seeing the "markers" returned is because you are looking at the first element of the matches returned, which is always the full matched pattern.  If you want to get rid of the "markers" you need to capture the parts you specifically want (using parenthesis) and look at the additional elements.  For example:

 

$string = "XblahX";
preg_match('~X.*?X~',$string,$match);

 

This will give you just

 

$match[0] => "XblahX"

 

But to capture just the "blah" you would do:

 

$string = "XblahX";
preg_match('~X(.*?)X~',$string,$match);

 

This would return

 

$match[0] => "XblahX"

$match[1] => "blah"

 

And the specific part (minus the "markers") would be in $match[1].

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.