简体   繁体   中英

PHP preg replace to change last two character in whole string

I have a problem in preg_replace.

I have a text file which includes:

#define FWTE_BLACK "{000000FF
#define FWTE_TEST "{006600FF
...

The complete text is here : https://eval.in/133942

I would like to replace the ending FF on each line to }" Like this :

#define FWTE_BLACK "{000000}"
#define FWTE_TEST "{006600}"
...

您可以尝试以下方法:

preg_replace('/FF$/m', '}"', $file_string);

尝试这个:

$content = preg_replace('#FF$#', '}"' $content);

Pattern (Here I check )

/\w{2}$/

Check this Demo CodeViper

PHP

<?php   
    $str = "#define FWTE_TEST \"{006600FF";
    $pattern = '/\w{2}$/';
    $replacement = '}"';  

    echo preg_replace($pattern, $replacement, $str);
?>

Result

#define FWTE_TEST "{006600}"

Hope this help you!

Try :

preg_replace('/(.*)[F]{2}$/','$1}"','#define FWTE_BLACK "{000000FF');

Based on your comment this is the update:

echo preg_replace('/(.*)[F]{2}([\n\r])+/','$1}"$2',$myString);

http://ideone.com/UOsenT

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