php - str_replace only a certain part of printed string? -


i have ran problem. want replace part in parenthesis, problem if value in parenthesis 1 or else in same part ([x(xxx:x:x)xx];), end replacing also, need x(value) @ end of each part.

[0(267:0:0)x1];[1(257:0:0)x1];[2(256:0:0)x1];[3(258:0:0)x1];[4(261:0:0)x1];   [5(262:0:0)x64];[6(320:0:0)x10];[7(17:0:0)x32];[8(295:0:0)x2];[9(35:0:0)x2];[10(44:1:1)x6];[11(0:0:0)x1];[12(0:0:0)x1];[13(0:0:0)x1];[14(0:0:0)x1];[15(0:0:0)x1];[16(0:0:0)x1];[17(0:0:0)x1];[18(0:0:0)x1];[19(0:0:0)x1];[20(0:0:0)x1];[21(0:0:0)x1];[22(0:0:0)x1];[23(0:0:0)x1];[24(0:0:0)x1];[25(0:0:0)x1];[26(0:0:0)x1];[27(0:0:0)x1];[28(0:0:0)x1];[29(0:0:0)x1];[30(0:0:0)x1];[31(0:0:0)x1];[32(0:0:0)x1];[33(0:0:0)x1];[34(0:0:0)x1];[35(0:0:0)x1];[103(0:0:0)x1];[102(0:0:0)x1];[101(0:0:0)x1];[100(0:0:0)x1]; 

i cannot working. if try replace 1 value in part in parenthesis, ends changing of 1's on printed part.

also keep in mind zero's in parenthesis won't 0's.

here's tried:

$items = $row['inventory']; $test = str_replace('267', '<img href="../assets/images/items/iron_sword.png">', $items); echo $items; 

i tried 1 line of code see how work.

i'm trying replace part in parenthesis image (for example, first 1 [0(267:0:0)x1];. want replace (267:0:0) image...

if want replace (*:*:*) pattern same image, cant try preg_replace:

$str = "[0(267:0:0)x1];[1(257:0:0)x1];[2(256:0:0)x1];[3(258:0:0)x1];[4(261:0:0)x1];"; $res = preg_replace("/\(\d+\:\d+\:\d+\)/",'<img href="../assets/images/items/iron_sword.png">', $str); 

you can see result here: http://codepad.org/uijgkv6n

if want replace different item different image, need translate map this:

$from = array(     '(267:0:0)',     '(257:0:0)',     ... ); $to = array(     '<img href="../assets/images/items/iron_sword.png">',     '<img href="../assets/images/items/other.png">',     ... ); $res = str_replace($from, $to, $str); 

and if have other requirements, can modify code need.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -