简体   繁体   中英

Parse for string in string php

I have a problem with regular expressions in PHP.

I have a string: "NAPLAK ROSSO+S.ARG.+LACC.ARG.+NK", all of this words contain options like ROSSO,S.ARG, Are colors ROSSO = Red, s.arg = silver and so on.

Out of this string i need to generate a description like:

Material: Naplak, Color: Red, Silver.... ....

I thought, that there is going to be an array with laguage codes something like this:

$options = array(
    "ROSSO" => "COLOR_RED",
    "S.ARG" => "COLOR_SILVER"
);

Could you please help me to write a regular expression for this purpose?

Best regargs, RussianRoot.

If they all are delimited by + then why don't you use explode ?

That will give you an array with each element and then you can do whatever you want with it.

A different way is to use str_replace and replace "ROSSO" with "COLOR_RED" and so on.

I don't think you need regex for this. Probably easier and faster to do something like:

$item = explode(' ', "NAPLAK ROSSO+S.ARG.+LACC.ARG.+NK");
$args = explode('+', $item[1]);
$item = $item[0];

echo $item;

Gives us "NAPLAK"

print_r($args);

Gives us:

array(4) [ 'ROSSO', 'S.ARG', 'LACC.ARG.', 'NK' ];

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