Question

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}"
...
Was it helpful?

Solution 4

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

OTHER TIPS

You can try this:

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

Try this:

$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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top