I have a text document with a lot of lines. I need to remove everything from this document, but leave only the icon.xxxx_xxxx_xxx from each line. Tell me how you can do this?

Example of one line:

0 1 1 1 7 15 0 LineageWeapons.small_sword_m00_wp LineageWeaponsTex.small_sword_t00_wp 0 0 0 0 0 1 0 0 icon.weapon_small_sword_i00 -1 1600 8 0 5F006E 0 1 27 1 1 LineageWeapons.small_sword_m00_wp 1 1 LineageWeaponsTex.small_sword_t00_wp 4 ItemSound.sword_small_1 ItemSound.sword_big_7 ItemSound.sword_mid_6 ItemSound.public_sword_shing_4 ItemSound.itemdrop_sword ItemSound.itemequip_sword 10 8 6 1 0 8 0 0 0 0 379 0 1 1 1000 0 -1 0 0.00000000 0.00000000 0.00000000 1.00000000 1.00000000 LineageWeapons.rangesample 0.94999999 0.55000001 0.55000001 11.00000000 0.00000000 0.00000000 -1 -1 -1 -1 -1 -1 

    2 answers 2

    To delete the data, you can use regular expressions. If the file size is not very large, you can extract all of its contents using the file_get_contents() function, extract the necessary data into an array and write the array of the required values ​​into a file using the file_put_contents() function

     <?php $filename = 'index.txt'; $content = file_get_contents($filename); $pattern = '/icon\.[^\s]+/'; preg_match_all($pattern, $content, $out); file_put_contents($filename, implode(PHP_EOL, $out[0])); 

    Or, if the file is large, you can open the temporary file, rewrite the contents of the first file line by line, and after closing both files, copy the new file to the old one

     <?php $filename = 'index.txt'; $filetemp = tempnam('/tmp', 'temp'); $fd = fopen($filename, 'r+'); $ft = fopen($filetemp, 'w+'); $pattern = '/icon\.[^\s]+/'; if($fd && $ft) { while (($line = fgets($fd, 4096)) !== false) { if(preg_match($pattern, $line, $out)) { fwrite($ft, $out[0].PHP_EOL); } } fclose($fd); fclose($ft); copy($filetemp, $filename); } 
    • The first code helped, but not much is needed. The code selects all the icons - and there may be several of them in the string. How to make the code delete everything just before 1 mentioning icons in the line? Well, everything else that comes after? - Elizaveta
    • @ Elizabeth try the second option, it describes the behavior you need. - cheops
    • Thank you!! - Elizabeth

    Option with preg_replace () and modifier m -

    The m modifier indicates that the text being searched should be treated as consisting of several lines. By default, the regular expression engine treats the text as a single line, regardless of what it really is. Accordingly, the metacharacters '^' and '$' indicate the beginning and end of the entire text. If this modifier is specified, then they will indicate respectively the beginning and end of each line of text.

     <?php $filename = 'index.txt'; $content = file_get_contents($filename); $content = preg_replace('%^.*?(icon\.[^\s]+).*+$%m', '$1', $content); file_put_contents($filename, $content); 

    The regular expression %^.*?(icon\.[^\s]+).*+$%m captures entirely every line that has the required substring icon.xxxx_xxxx_xxx (capture group (icon\.[^\s]+) ). In this case, only the first substring is included in the capture group, since it uses a lazy enumeration of characters .*? . And accordingly, the function preg_replace () changes the captured string to the capture group $1 .