SQL query to replace string based on wildcardt
I would like to run this type of query on my WP database to remove all span instances with id="more-":
UPDATE wp_posts SET post_content = REPLACE (
post_content,
'<p><span id="more-35075"></span></p>',
'');
But the number that follows the 'more-' in my example is a variable. How to write this query with someting like a wildcard: span id="more-*.
Thank you
mysql wordpress replace phpmyadmin mariadb
|
show 4 more comments
I would like to run this type of query on my WP database to remove all span instances with id="more-":
UPDATE wp_posts SET post_content = REPLACE (
post_content,
'<p><span id="more-35075"></span></p>',
'');
But the number that follows the 'more-' in my example is a variable. How to write this query with someting like a wildcard: span id="more-*.
Thank you
mysql wordpress replace phpmyadmin mariadb
Are you using MySQL server version 8.0 and above ?
– Madhur Bhaiya
Nov 13 '18 at 16:14
A better question imo is why you're storing html in a database vs storing just the id and the post content. Then you can use your application to format the HTML
– Rogue
Nov 13 '18 at 16:15
Manipulating HTML with MySQL may work in extremely trivial cases, but anything even remotely complex is basically impossible without a proper HTML parser.
– tadman
Nov 13 '18 at 16:17
@Rogue he is not storing. Wordpress is storing it (table name iswp_posts
); most likely OP has no control over it.
– Madhur Bhaiya
Nov 13 '18 at 16:19
Surely whatever wordpress plugin he is using has some way of being manipulated to not do ... well awful things?
– Rogue
Nov 13 '18 at 16:20
|
show 4 more comments
I would like to run this type of query on my WP database to remove all span instances with id="more-":
UPDATE wp_posts SET post_content = REPLACE (
post_content,
'<p><span id="more-35075"></span></p>',
'');
But the number that follows the 'more-' in my example is a variable. How to write this query with someting like a wildcard: span id="more-*.
Thank you
mysql wordpress replace phpmyadmin mariadb
I would like to run this type of query on my WP database to remove all span instances with id="more-":
UPDATE wp_posts SET post_content = REPLACE (
post_content,
'<p><span id="more-35075"></span></p>',
'');
But the number that follows the 'more-' in my example is a variable. How to write this query with someting like a wildcard: span id="more-*.
Thank you
mysql wordpress replace phpmyadmin mariadb
mysql wordpress replace phpmyadmin mariadb
edited Nov 13 '18 at 17:38
Madhur Bhaiya
19.6k62236
19.6k62236
asked Nov 13 '18 at 16:12
APEWAWAPEWAW
84
84
Are you using MySQL server version 8.0 and above ?
– Madhur Bhaiya
Nov 13 '18 at 16:14
A better question imo is why you're storing html in a database vs storing just the id and the post content. Then you can use your application to format the HTML
– Rogue
Nov 13 '18 at 16:15
Manipulating HTML with MySQL may work in extremely trivial cases, but anything even remotely complex is basically impossible without a proper HTML parser.
– tadman
Nov 13 '18 at 16:17
@Rogue he is not storing. Wordpress is storing it (table name iswp_posts
); most likely OP has no control over it.
– Madhur Bhaiya
Nov 13 '18 at 16:19
Surely whatever wordpress plugin he is using has some way of being manipulated to not do ... well awful things?
– Rogue
Nov 13 '18 at 16:20
|
show 4 more comments
Are you using MySQL server version 8.0 and above ?
– Madhur Bhaiya
Nov 13 '18 at 16:14
A better question imo is why you're storing html in a database vs storing just the id and the post content. Then you can use your application to format the HTML
– Rogue
Nov 13 '18 at 16:15
Manipulating HTML with MySQL may work in extremely trivial cases, but anything even remotely complex is basically impossible without a proper HTML parser.
– tadman
Nov 13 '18 at 16:17
@Rogue he is not storing. Wordpress is storing it (table name iswp_posts
); most likely OP has no control over it.
– Madhur Bhaiya
Nov 13 '18 at 16:19
Surely whatever wordpress plugin he is using has some way of being manipulated to not do ... well awful things?
– Rogue
Nov 13 '18 at 16:20
Are you using MySQL server version 8.0 and above ?
– Madhur Bhaiya
Nov 13 '18 at 16:14
Are you using MySQL server version 8.0 and above ?
– Madhur Bhaiya
Nov 13 '18 at 16:14
A better question imo is why you're storing html in a database vs storing just the id and the post content. Then you can use your application to format the HTML
– Rogue
Nov 13 '18 at 16:15
A better question imo is why you're storing html in a database vs storing just the id and the post content. Then you can use your application to format the HTML
– Rogue
Nov 13 '18 at 16:15
Manipulating HTML with MySQL may work in extremely trivial cases, but anything even remotely complex is basically impossible without a proper HTML parser.
– tadman
Nov 13 '18 at 16:17
Manipulating HTML with MySQL may work in extremely trivial cases, but anything even remotely complex is basically impossible without a proper HTML parser.
– tadman
Nov 13 '18 at 16:17
@Rogue he is not storing. Wordpress is storing it (table name is
wp_posts
); most likely OP has no control over it.– Madhur Bhaiya
Nov 13 '18 at 16:19
@Rogue he is not storing. Wordpress is storing it (table name is
wp_posts
); most likely OP has no control over it.– Madhur Bhaiya
Nov 13 '18 at 16:19
Surely whatever wordpress plugin he is using has some way of being manipulated to not do ... well awful things?
– Rogue
Nov 13 '18 at 16:20
Surely whatever wordpress plugin he is using has some way of being manipulated to not do ... well awful things?
– Rogue
Nov 13 '18 at 16:20
|
show 4 more comments
1 Answer
1
active
oldest
votes
In MySQL version 8.0 and above, you can use REGEX_REPLACE()
function. In absence of the same, some complicated string operations can be done. This is based on your confirmation, that the said sub-string is occurring only once in a value.
REPLACE()
does not have any support for wildcards, patterns, regular expressions etc. It only replaces a given fixed substring with another fixed substring, in a bigger string.
Instead, we can try to extract portions of the post_content
. We will extract the leading substring before the '<p><span id="more-'
using Substring_Index()
function. Similarly, we will extract the trailing substring after the '"></span></p>'
portion.
Now, we can simply Concat()
these portions to get the required post_content
. You can find details of various String functions used here: https://dev.mysql.com/doc/refman/8.0/en/string-functions.html
I have also added a WHERE
condition, so that we pick only those rows which match our given substring criteria.
UPDATE wp_posts
SET post_content =
CONCAT(
SUBSTRING_INDEX(post_content,
'<p><span id="more-',
1),
SUBSTRING(post_content,
LOCATE('"></span></p>',
post_content,
LOCATE('<p><span id="more-',
post_content)
) + 13) -- 13 is character length of "></span></p>
)
WHERE post_content LIKE '%<p><span id="more-%"></span></p>%';
Query #1: Data before Update operations
SELECT * FROM wp_posts;
| post_content |
| ------------------------------------------------------- |
| adasdaadsa<p><span id="more-35075"></span></p>121324124 |
| 1412123123<p><span id="more-232"></span></p>adasdaafas |
Query #2: Data after Update operations
SELECT * FROM wp_posts;
| post_content |
| -------------------- |
| adasdaadsa121324124 |
| 1412123123adasdaafas |
View on DB Fiddle
Hello Madhur. Thank you for your help! I launched the SQL query but I get the message: 0 rows affected.
– APEWAW
Nov 14 '18 at 10:48
@APEWAW can you setup a db-fiddle.com Also check the DB fiddle links I have shared. Maybe there is something different in your sample data.
– Madhur Bhaiya
Nov 14 '18 at 10:49
1
My fault! There were not always <p> tags, so I adapted the query and it worked perfectly. A big thank you for your very valuable help!
– APEWAW
Nov 14 '18 at 14:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285104%2fsql-query-to-replace-string-based-on-wildcardt%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In MySQL version 8.0 and above, you can use REGEX_REPLACE()
function. In absence of the same, some complicated string operations can be done. This is based on your confirmation, that the said sub-string is occurring only once in a value.
REPLACE()
does not have any support for wildcards, patterns, regular expressions etc. It only replaces a given fixed substring with another fixed substring, in a bigger string.
Instead, we can try to extract portions of the post_content
. We will extract the leading substring before the '<p><span id="more-'
using Substring_Index()
function. Similarly, we will extract the trailing substring after the '"></span></p>'
portion.
Now, we can simply Concat()
these portions to get the required post_content
. You can find details of various String functions used here: https://dev.mysql.com/doc/refman/8.0/en/string-functions.html
I have also added a WHERE
condition, so that we pick only those rows which match our given substring criteria.
UPDATE wp_posts
SET post_content =
CONCAT(
SUBSTRING_INDEX(post_content,
'<p><span id="more-',
1),
SUBSTRING(post_content,
LOCATE('"></span></p>',
post_content,
LOCATE('<p><span id="more-',
post_content)
) + 13) -- 13 is character length of "></span></p>
)
WHERE post_content LIKE '%<p><span id="more-%"></span></p>%';
Query #1: Data before Update operations
SELECT * FROM wp_posts;
| post_content |
| ------------------------------------------------------- |
| adasdaadsa<p><span id="more-35075"></span></p>121324124 |
| 1412123123<p><span id="more-232"></span></p>adasdaafas |
Query #2: Data after Update operations
SELECT * FROM wp_posts;
| post_content |
| -------------------- |
| adasdaadsa121324124 |
| 1412123123adasdaafas |
View on DB Fiddle
Hello Madhur. Thank you for your help! I launched the SQL query but I get the message: 0 rows affected.
– APEWAW
Nov 14 '18 at 10:48
@APEWAW can you setup a db-fiddle.com Also check the DB fiddle links I have shared. Maybe there is something different in your sample data.
– Madhur Bhaiya
Nov 14 '18 at 10:49
1
My fault! There were not always <p> tags, so I adapted the query and it worked perfectly. A big thank you for your very valuable help!
– APEWAW
Nov 14 '18 at 14:13
add a comment |
In MySQL version 8.0 and above, you can use REGEX_REPLACE()
function. In absence of the same, some complicated string operations can be done. This is based on your confirmation, that the said sub-string is occurring only once in a value.
REPLACE()
does not have any support for wildcards, patterns, regular expressions etc. It only replaces a given fixed substring with another fixed substring, in a bigger string.
Instead, we can try to extract portions of the post_content
. We will extract the leading substring before the '<p><span id="more-'
using Substring_Index()
function. Similarly, we will extract the trailing substring after the '"></span></p>'
portion.
Now, we can simply Concat()
these portions to get the required post_content
. You can find details of various String functions used here: https://dev.mysql.com/doc/refman/8.0/en/string-functions.html
I have also added a WHERE
condition, so that we pick only those rows which match our given substring criteria.
UPDATE wp_posts
SET post_content =
CONCAT(
SUBSTRING_INDEX(post_content,
'<p><span id="more-',
1),
SUBSTRING(post_content,
LOCATE('"></span></p>',
post_content,
LOCATE('<p><span id="more-',
post_content)
) + 13) -- 13 is character length of "></span></p>
)
WHERE post_content LIKE '%<p><span id="more-%"></span></p>%';
Query #1: Data before Update operations
SELECT * FROM wp_posts;
| post_content |
| ------------------------------------------------------- |
| adasdaadsa<p><span id="more-35075"></span></p>121324124 |
| 1412123123<p><span id="more-232"></span></p>adasdaafas |
Query #2: Data after Update operations
SELECT * FROM wp_posts;
| post_content |
| -------------------- |
| adasdaadsa121324124 |
| 1412123123adasdaafas |
View on DB Fiddle
Hello Madhur. Thank you for your help! I launched the SQL query but I get the message: 0 rows affected.
– APEWAW
Nov 14 '18 at 10:48
@APEWAW can you setup a db-fiddle.com Also check the DB fiddle links I have shared. Maybe there is something different in your sample data.
– Madhur Bhaiya
Nov 14 '18 at 10:49
1
My fault! There were not always <p> tags, so I adapted the query and it worked perfectly. A big thank you for your very valuable help!
– APEWAW
Nov 14 '18 at 14:13
add a comment |
In MySQL version 8.0 and above, you can use REGEX_REPLACE()
function. In absence of the same, some complicated string operations can be done. This is based on your confirmation, that the said sub-string is occurring only once in a value.
REPLACE()
does not have any support for wildcards, patterns, regular expressions etc. It only replaces a given fixed substring with another fixed substring, in a bigger string.
Instead, we can try to extract portions of the post_content
. We will extract the leading substring before the '<p><span id="more-'
using Substring_Index()
function. Similarly, we will extract the trailing substring after the '"></span></p>'
portion.
Now, we can simply Concat()
these portions to get the required post_content
. You can find details of various String functions used here: https://dev.mysql.com/doc/refman/8.0/en/string-functions.html
I have also added a WHERE
condition, so that we pick only those rows which match our given substring criteria.
UPDATE wp_posts
SET post_content =
CONCAT(
SUBSTRING_INDEX(post_content,
'<p><span id="more-',
1),
SUBSTRING(post_content,
LOCATE('"></span></p>',
post_content,
LOCATE('<p><span id="more-',
post_content)
) + 13) -- 13 is character length of "></span></p>
)
WHERE post_content LIKE '%<p><span id="more-%"></span></p>%';
Query #1: Data before Update operations
SELECT * FROM wp_posts;
| post_content |
| ------------------------------------------------------- |
| adasdaadsa<p><span id="more-35075"></span></p>121324124 |
| 1412123123<p><span id="more-232"></span></p>adasdaafas |
Query #2: Data after Update operations
SELECT * FROM wp_posts;
| post_content |
| -------------------- |
| adasdaadsa121324124 |
| 1412123123adasdaafas |
View on DB Fiddle
In MySQL version 8.0 and above, you can use REGEX_REPLACE()
function. In absence of the same, some complicated string operations can be done. This is based on your confirmation, that the said sub-string is occurring only once in a value.
REPLACE()
does not have any support for wildcards, patterns, regular expressions etc. It only replaces a given fixed substring with another fixed substring, in a bigger string.
Instead, we can try to extract portions of the post_content
. We will extract the leading substring before the '<p><span id="more-'
using Substring_Index()
function. Similarly, we will extract the trailing substring after the '"></span></p>'
portion.
Now, we can simply Concat()
these portions to get the required post_content
. You can find details of various String functions used here: https://dev.mysql.com/doc/refman/8.0/en/string-functions.html
I have also added a WHERE
condition, so that we pick only those rows which match our given substring criteria.
UPDATE wp_posts
SET post_content =
CONCAT(
SUBSTRING_INDEX(post_content,
'<p><span id="more-',
1),
SUBSTRING(post_content,
LOCATE('"></span></p>',
post_content,
LOCATE('<p><span id="more-',
post_content)
) + 13) -- 13 is character length of "></span></p>
)
WHERE post_content LIKE '%<p><span id="more-%"></span></p>%';
Query #1: Data before Update operations
SELECT * FROM wp_posts;
| post_content |
| ------------------------------------------------------- |
| adasdaadsa<p><span id="more-35075"></span></p>121324124 |
| 1412123123<p><span id="more-232"></span></p>adasdaafas |
Query #2: Data after Update operations
SELECT * FROM wp_posts;
| post_content |
| -------------------- |
| adasdaadsa121324124 |
| 1412123123adasdaafas |
View on DB Fiddle
edited Nov 13 '18 at 17:39
answered Nov 13 '18 at 17:31
Madhur BhaiyaMadhur Bhaiya
19.6k62236
19.6k62236
Hello Madhur. Thank you for your help! I launched the SQL query but I get the message: 0 rows affected.
– APEWAW
Nov 14 '18 at 10:48
@APEWAW can you setup a db-fiddle.com Also check the DB fiddle links I have shared. Maybe there is something different in your sample data.
– Madhur Bhaiya
Nov 14 '18 at 10:49
1
My fault! There were not always <p> tags, so I adapted the query and it worked perfectly. A big thank you for your very valuable help!
– APEWAW
Nov 14 '18 at 14:13
add a comment |
Hello Madhur. Thank you for your help! I launched the SQL query but I get the message: 0 rows affected.
– APEWAW
Nov 14 '18 at 10:48
@APEWAW can you setup a db-fiddle.com Also check the DB fiddle links I have shared. Maybe there is something different in your sample data.
– Madhur Bhaiya
Nov 14 '18 at 10:49
1
My fault! There were not always <p> tags, so I adapted the query and it worked perfectly. A big thank you for your very valuable help!
– APEWAW
Nov 14 '18 at 14:13
Hello Madhur. Thank you for your help! I launched the SQL query but I get the message: 0 rows affected.
– APEWAW
Nov 14 '18 at 10:48
Hello Madhur. Thank you for your help! I launched the SQL query but I get the message: 0 rows affected.
– APEWAW
Nov 14 '18 at 10:48
@APEWAW can you setup a db-fiddle.com Also check the DB fiddle links I have shared. Maybe there is something different in your sample data.
– Madhur Bhaiya
Nov 14 '18 at 10:49
@APEWAW can you setup a db-fiddle.com Also check the DB fiddle links I have shared. Maybe there is something different in your sample data.
– Madhur Bhaiya
Nov 14 '18 at 10:49
1
1
My fault! There were not always <p> tags, so I adapted the query and it worked perfectly. A big thank you for your very valuable help!
– APEWAW
Nov 14 '18 at 14:13
My fault! There were not always <p> tags, so I adapted the query and it worked perfectly. A big thank you for your very valuable help!
– APEWAW
Nov 14 '18 at 14:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285104%2fsql-query-to-replace-string-based-on-wildcardt%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you using MySQL server version 8.0 and above ?
– Madhur Bhaiya
Nov 13 '18 at 16:14
A better question imo is why you're storing html in a database vs storing just the id and the post content. Then you can use your application to format the HTML
– Rogue
Nov 13 '18 at 16:15
Manipulating HTML with MySQL may work in extremely trivial cases, but anything even remotely complex is basically impossible without a proper HTML parser.
– tadman
Nov 13 '18 at 16:17
@Rogue he is not storing. Wordpress is storing it (table name is
wp_posts
); most likely OP has no control over it.– Madhur Bhaiya
Nov 13 '18 at 16:19
Surely whatever wordpress plugin he is using has some way of being manipulated to not do ... well awful things?
– Rogue
Nov 13 '18 at 16:20