Hi, I have a text file called keywords.txt and I want to search the $text variable for the keywords in the keywords.txt file and replace those kwywords with the <b>,</b> tags to bold them in the output. This is the piece of code I wrote. everything works fine, except that in the output, only the last keyword specified in the keywords.txt file gets bolded. I've tried different loop techniques and methods to get all the keywords in the txt file to be bolded - still no luck. So I guess the problem is logical. Any help is appreciated. Thanks!
Try this: <?php $handle = @fopen(ABSPATH."wp-content/plugins/keywords.txt", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle, 4096); echo "<b">$buffer</b>, "; } fclose($handle); } ?> Code (markup):
Try this: $c= file_get_contents('keywords.txt'); preg_replace('/['.$text.']/', '<b>'.$text.</b>',$c); I'm not very good with pregs, but I think the above will work.
My guess is that there's some kind of whitespace or other character(s) preventing the other lines from matching. Try inserting a $line = trim($line); before the str_replace line.
Thank you all for your wonderful support. I tried all of your suggestions and... Shoro, thanks a lot! That worked. The prob was in the whitespace.
Almost, you need to remove the [ ] completely and it will work fine. if you were to use braces, it would be ( ) instead