简体   繁体   中英

php - Regular Expression to match someting that starts with

I am terribly sorry but the array looks like this:

Array
(
[1] => Array
    (
        [0] => msie6.0
        [1] => 7
    )

[2] => Array
    (
        [0] => safari5.0.3
        [1] => 5
    )

[3] => Array
    (
        [0] => chrome18.0.1025.308
        [1] => 1
    )

[4] => Array
    (
        [0] => firefox20.0
        [1] => 4
    )
[5] => Array
    (
        [0] => msie7.0
        [1] => 915
    )

and so on... When i try to replace for example msie6.0 and msie7.0 with InternetExplorer and add it occurence :

preg_match("/#^msie(.*)$#i/is", $results, $matches);
$test = $matches[0] ;
print_array($test);
$results["#^startText(.*)$#i"] = $results['InternetExplorer'];
print_array($results);
unset($results["/#^msie(.*)$#i/is]);

it not match the perfect as i want. any solution for that ? in order to have :

Array
(
[1] => Array
    (
        [0] => InternetExplorer
        [1] => 922
    )

[2] => Array
    (
        [0] => safari5.0.3
        [1] => 5
    )

[3] => Array
    (
        [0] => chrome18.0.1025.308
        [1] => 1
    )

[4] => Array
    (
        [0] => firefox20.0
        [1] => 4
    )

After your clarification in comment. I think you don't need regex, strpos is enough too that job.

$rows["InternetExplorer"] = 0;
foreach($rows as $key => $value){
  if(strpos($key,"msie") !== false){
     $rows["InternetExplorer"] += $value;
     unset($rows[$key]);
  }
}

DEMO .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM