Nyla Posted November 22, 2014 Share Posted November 22, 2014 For some reason, this doesn't work: <?php$string = 'You have a code CODE code of ethics';while (preg_match("/code\s+code/",$string)) { $string = preg_replace("/code\s+code/","code",$string); } print $string;?> I don't want to replace all instances of duplicated words, rather, only when the word "code" is duplicated. Throughout my 100mb text file, the word "code" appears duplicated (with varying whitespace between). For example: From this: You have a code CODE code code of ethics. Code code Code green means everything is okay. What is the CODE code code code for the lock. To this: You have a code of ethics. Code green means everything is okay. What is the code for the lock. But for some reason, I cannot get my preg_replace to work? Quote Link to comment https://forums.phpfreaks.com/topic/292632-remove-duplicate-word/ Share on other sites More sharing options...
requinix Posted November 22, 2014 Share Posted November 22, 2014 Wtf happened to create all those "code"s? Regular expressions are case-sensitive by default. Use the /i flag to stop that. While I'm here, you don't need to use a loop for this. <?php $string = 'You have a code CODE code of ethics'; $string = preg_replace('/code(\s+code)+/i', "code", $string); // "code" and one or more of (whitespace and "code") print $string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/292632-remove-duplicate-word/#findComment-1497276 Share on other sites More sharing options...
Nyla Posted November 22, 2014 Author Share Posted November 22, 2014 Nope, still BIG problem. On your computer, create a text file called "bigfile.txt" with ONE string on it, exactly: Very Good Ethics Code CODE CODE. Now, I run this code (I didn't leave anything out): <?php #again, "bigfile.txt" is a text file with this exact string: Very Good Ethics Code CODE CODE. $lines = file("bigfile.txt"); foreach ($lines as $n => $line) { $line = preg_replace('/code(\s+code)+/i', "code", $line); } file_put_contents("bigfile.txt", $lines); ?> Nothing got changed (here's the output): Very Good Ethics Code CODE CODE. If you help me with this, I will buy you a beer (non-alcoholic, because I'm a goody-goody person) if you ever come to my hometown! Quote Link to comment https://forums.phpfreaks.com/topic/292632-remove-duplicate-word/#findComment-1497281 Share on other sites More sharing options...
requinix Posted November 22, 2014 Share Posted November 22, 2014 When you modify $line you don't actually modify $lines too. Just a copy of $line. $lines[$n] = preg_replace('/code(\s+code)+/i', "code", $line);Surprised you forgot that since it was brought up in another thread and you had the right code there. Quote Link to comment https://forums.phpfreaks.com/topic/292632-remove-duplicate-word/#findComment-1497285 Share on other sites More sharing options...
Nyla Posted November 22, 2014 Author Share Posted November 22, 2014 Thank you, Requinex! You have saved my bacon MANY times now! I sure appreciate it! One non-alcoholic beer comin' right up! Quote Link to comment https://forums.phpfreaks.com/topic/292632-remove-duplicate-word/#findComment-1497288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.