简体   繁体   中英

php replace with regex and remove specified pattern with regex

|affffc100|Hitem:bb:101:1:1:1:1:48:-30:47:18:5:2:6:6:0:0:0:0:0:0:0:0|h[Subject Name]|h|r

my usual printed out variable is ^

|cffffc700|Hitem:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x|h[SUBJECT_NAME]|h|r

my pattern is ^

ALL X's can be aZ, 0-9

in one column I have many variables like that (up to 8). and all variables are mixed with strings like that:

|affffc100|Hitem:bb:101:1:1:1:1:48:-30:47:18:5:2:6:6:0:0:0:0:0:0:0:0|h[Gold]|h|r NEW SOLD |affffc451|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Copper]|h|r maximum price 15k|affffx312|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Silver]|h|r 

In one variable I want to clean all these unnecessary patterns and leave only subject name in brackets. []

So;

|cffffc700|Hitem:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x|h[SUBJECT NAME]|h|r

needs to leave only SUBJECT_NAME in my variable. just to remind, I have always more than one from these pattern in my every variable... (up to 8) I've searched it everywhere but couldn't find any reasonable answers NOR good patterns. Tried to make it myself but I guess I need to take all these patterns and make it array and clean it and only leave these subject names but I don't know exactly how to do it.

how do I convert this to :

|affffc100|Hitem:bb:101:1:1:1:1:48:-30:47:18:5:2:6:6:0:0:0:0:0:0:0:0|h[Gold]|h|r NEW SOLD |affffc451|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Copper]|h|r maximum price 15k|affffx312|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Silver]|h|r 

this:

Gold NEW SOLD Copper maxiumum price 15k Silver

what should I use, preg_replace?


one more thing left, when I have a string without my special pattern, I get empty result from the function eg:

$str = "15KKK sold, 20KK updated";

expected result:

"15KKK sold, 20KK updated" // same without any pattern

but ^ that one returns EMPTY result..

another string:

$str = "|affffc100|Hitem:bb:101:1:1:1:1:48:-30:47:18:5:2:6:6:0:0:0:0:0:0:0:0|h[Uranium]|h|r 155kk |affffc451|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Metal]|h|r is sold";

expected result:

"Uranium 155kk Metal is sold"

if I use that function with non-pattern string it returns empty result that's my problem now

thank you very much

try this regex:

\|\w{9}\|Hitem(?::-?\w+)+\|h\[(?<SUBJECTNAME>\w+)\]\|h\|r

it will capture each variable sequence, as well as the relevant element name in the named group.

see the demo here

I'd do:

$str = '|affffc100|Hitem:bb:101:1:1:1:1:48:-30:47:18:5:2:6:6:0:0:0:0:0:0:0:0|h[Gold]|h|r NEW SOLD |affffc451|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Copper]|h|r maximum price 15k|affffx312|Hitem:bb:101:1:1:1:1:25:-33:12:42:5a:2f:6w:6:0:0:0:0f:0:0a:0b:0|h[Silver]|h|r';

preg_match_all('/h(\[.+?\])\|h\|r([^|]*)/', $str, $m);
for($i=0; $i<count($m[0]); $i++) {
    $res .= $m[1][$i] . ' '  . $m[2][$i] . ' ';
}
echo $res,"\n";

Output:

[Gold]  NEW SOLD  [Copper]  maximum price 15k [Silver]  

If you want to keep the strings that don't match, test the result of preg_match:

if (preg_match_all('/h(\[.+?\])\|h\|r([^|]*)/', $str, $m)) {
    for($i=0; $i<count($m[0]); $i++) {
        $res .= $m[1][$i] . ' '  . $m[2][$i] . ' ';
    }
} else {
    $res = $str;
}
echo $res,"\n";

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