Mysql – Need to replace only first occurrence of string

MySQLreplace

I hope I worded the question right. Here are the specifics:

This is regarding a MySQL database. I've inherited several hundred posts with custom fields and a unique excerpt. At this point I think I can solve the problem by inserting the <!--More--> tag into each post at a specific point. I need to do a search and replace with the following parameters:

  1. Search each post_content field for the first occurrence of </em>
  2. Insert the <!--More--> tag right after the </em>

Unfortunately each post has multiple <em> tag pairs, and if a post has more than one <!--More--> tag in it, WordPress just ignores all of them. So I need to so some sort of search and replace, but I have had no success after two days of trying every possible code snippet, SQL query and plugin I could find.

Best Answer

This may come across as silly, but if the CMS ignores subsequent <!--more--> tags, you shouldn't really care if it adds one after every </em> closing tag. Sure it may make the posts slightly larger than they should be, but since they're just going to be ignored, it seems silly to spend two days trying to not do that when at the end of the day it doesn't really matter whether you do or not.

That said, I am not a MySQL guy, but looks like this is as decent an approach as any, from this StackOverflow answer:

UPDATE wp_posts
 SET post_content = CONCAT(REPLACE(LEFT(post_content, 
   INSTR(post_content, '</em>')+4), '</em>', '</em><!--more-->'), 
   SUBSTRING(post_content, INSTR(post_content, '</em>') + 5))
 WHERE INSTR(post_content, '</em>') > 0;

or the (slightly simpler):

UPDATE wp_posts
 SET post_content = CONCAT(LEFT(post_content, INSTR(post_content, '</em>')-1),
                           '</em><!--more-->',
                           SUBSTRING(post_content, INSTR(post_content, '</em>')+ 5))
 WHERE INSTR(post_content, '</em>') > 0;

Before you do that you may want to check to see how many rows this will affect:

SELECT COUNT(*)
  FROM wp_posts
  WHERE INSTR(post_content, '</em>') > 0;

You'll probably want to add a WHERE clause to only identify those posts that actually contain an </em> tag (and you may need to define the requirements for what to do in those cases).