简体   繁体   中英

PHP replace and looping character

How to replace character and looping character in PHP?

For example I have data input form textarea like this :

#CI
23,45,56x8 788,98,554x7 100x100

or 

@CI
23.45.56x8 788.98.554x7 100x100

My teacher said choose one of the above data format and replace that to output into something like this:

CI,23,8
CI,45,8
CI,56,8
CI,788,7
CI,98,7
CI,554,7
CI,100,100

My code

<?php 

    $input = 
    "@CI
    23,45,56x8 788,98,554x7 100x100";

    $reg = "/((?:\d+,)*(?:\d+))x(\d+)/";
    $new = preg_match($reg, $input);
    echo $new;


 ?>

such regex as you can see at demo link give you good data structure

((?:\d+,)*(?:\d+))x(\d+)

result

MATCH 1
1.  [0-8]   `23,45,56`
2.  [9-10]  `8`
MATCH 2
1.  [11-21] `788,98,554`
2.  [22-23] `7`
MATCH 3
1.  [24-27] `100`
2.  [28-31] `100`

demolimk

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