I have a WordPress website and following a change of theme I have to modify the structure of a thousand links present on about fifty pages. I would like to do it in SQL via PHPMYADMIN.
Is there a way in SQL to remove the end of all my link with the following structure :
- <a href="https://website.com/cours/les-problemes/lecon/s1-2014-2015-mathematiques-les-problemes/">
- <a href="https://website.com/cours/la-division/lecon/s3-2014-2015-mathematiques-la-division-n-nakatani/">
- <a href="https://website.com/cours/mathematiques-larithmetique/lecon/201819-s5-fa-mathematiques-nathalie-nakatani/">
In order to only get :
- <a href="https://website.com/cours/les-problemes/">
- <a href="https://website.com/cours/la-division/">
- <a href="https://website.com/cours/mathematiques-larithmetique/">
I tried to use the answer of this topic : MYSQL Replace string between two known strings but I did not manage to find a solution to fit my purpose.
I also thought about doing it in two parts :
1- Remove the content between the '/lecon/' and the '">'.
2- Then remove completely all the iteration of '/lecon/' of my pages, because they only occur on the links that I want to edit.
But my knowledges in SQL are limited and I have no clue of how to do the first part.
My apologizes for my English.
Thanks in avance for any helps !
On MySQL 8+, we can try using a regex replacement:
SELECT
tag,
REGEXP_REPLACE(tag, '(<a href="https?://(?:[^/]+/){3}).*">', '$1">')
FROM yourTable;
Demo
For MySQL < 8 (5.7), without REGEXP_REPLACE:
SELECT REPLACE(CONCAT(TRIM(TRAILING SUBSTRING_INDEX(url, '/lecon/', -1) FROM url), '">'), 'lecon/', '') FROM `your_table`
DEMO
Using your idea, I removed all from /lecon/ to the end in STEP 1 and concatenated "> to repair the HTML URL, and then I replaced lecon/ with an empty string in STEP 2.
Related
I'm trying replace this shortcode
[RICH_REVIEWS_SHOW category=”all” num=”6″]
with this shortcode
[site_reviews summary count="3" hide="date"] in a wordpress database.
I've tried the plugin "better search and replace" but no luck.
I've also tried using this code via the cpanel.
update wplq_posts set post_content =
replace(post_content,'[RICH_REVIEWS_SHOW category=”all” num=”6″]','[site_reviews summary count="3" hide="date"]');
Initially no matches, if I remove certain parts of the shortcode I get some results. I'm a bit lost as to what will get the job the job done.
Can anyone point me in the right direction.
Thanks in advance.
The SQL in your example is correct.
A likely explanation for the issue is that the double-quotes within the [RICH_REVIEWS_SHOW category=”all” num=”6″] do not match the double quotes used within the shortcode stored in the post_content table.
For example ” vs ″ vs "
I suggest finding a post manually that has this shortcode and copying that that shortcode into the SQL query. Then try again.
and thanks in advance for any help. I'm working on fixing all broken links in a massive WordPress multisite database and need some help writing an SQL query to run via PHP MyAdmin. I've searched, but can't the perfect solution...
PROBLEM: We have more than a thousand broken links that start with http:/ instead of http://
CHALLENGE: The following would result in numerous links starting with http:///
UPDATE wp_1_posts
SET post_content = replace (post_content,
'http:/',
'http://');
PROCESS: I want to write a query to SELECT all these links first, so I can review them to ensure I don't do any damage when replacing the text string. Downloading a db dump and doing a manual S&R is not an option since we're talking about a multi-gigabyte database.
I thought something like this would work...
SELECT * FROM wp_1_posts
WHERE post_content LIKE '%http:/%'
AND WHERE post_content NOT LIKE '%http://%'
But that just throws a syntax error. Am I even close?
QUESTION #1: How can I find all instances of "http:/" without returning all "http://" instances in the query results.
QUESTION #2: How might I safely fix all instances of "http:/" without affecting any "http://" strings.
FYI: I'll admit I know just enough about this to be dangerous, and I am not familiar with regular expressions. at. all. That's why I'm turning to you for help. Thanks again!
This should work, in MYSQL:
UPDATE wp_1_posts SET post_content = replace(post_content,'http:/', 'http://')
WHERE post_content REGEXP 'http:/[^/]'
I want to remove the first image of every post of my wordpress blog, so I installed the lib_mysqludf_preg library to use the preg_replace() function that performs a regular expression search and replaces using a PCRE pattern.
Once installed, I have tested it with this line of code to assure that everything works fine:
SELECT PREG_REPLACE('/fox/i' , '' , 'the quick brown fox' ,2 );
The result was as expected, so the installation is OK.
The problem is that I don't know how to use this function to replace the images that are stored in the column 'content_post' (at 'wordpress.wp_posts') for a blank space. I tried this expression:
SELECT PREG_REPLACE('/<img[^>]+\>/i', '', post_content, 1) FROM wordpress.wp_posts;
But this code returns me the whole lists of posts, not only of this database but of the rest of the databases I have installed in my local computer and doesn't replace any single image.
Any idea on what am I doing wrong?
Thanks in advance.
Ok, I've solved the problem. I post the code because it may be of use for someone with a similar doubt.
This is the code I have used:
UPDATE database_name.wp_posts
SET post_content=PREG_REPLACE( '/<img[^>]+\>/i', '' , post_content, 1 );
I have been trying to find how I can query the first sentence only of a paragraph within a field (HTML code) for SQL Server but I am unable to find how. I have found solutions for MySQL using the SUBSTRING_INDEX and I have also found solutions returning a certain number of words/characters but not using a specific delimiter.
My field is stored as HTML, an example is as follows:
<html><body>Enter the following page information.<br><br>
<b>Display #:</b> 1 [Automatically Populated]<br>
<b>Start Page: </b> 1 [Automatically Populated]<br>
<b>DCI Name:</b> DEMOG<br>
<b>Clinical Planned Event:</b> BASELINE1<br>
<font color="#0070C0">TAKE A SCREENSHOT</font>.<br>
</body></html>
In this example, I am hoping to only return/query "Enter the following page information" and not the rest of the paragraph. I'm assuming the HTML break might be the best delimiter as some sentences may end in a colon.
Thank you in advance! I hope I explained the scenario well enough.
I realize this is ugly as sin, but assuming that the first <br> is the end of the line, this should work in the SQL Server back-end:
DECLARE #x nvarchar(200)
SET #x = '<html><body>Enter the following page information.<br><br><b>Display #:</b>'
SELECT substring(#x,
(charindex('<br>', lower(#x)) - 1) -
(charindex('>', REVERSE(LEFT(#x, charindex('<br>', lower(#x)) - 1))))+2,
charindex('>', REVERSE(LEFT(#x, charindex('<br>', lower(#x)) - 1))) - 1
)
Basically, we find the last instance of > in the string before the first <br>, and then find the <br> at the end, and take the difference between the two for the length.
This could absolutely be written cleaner in a function, but I opted to go with pure T-SQL to avoid using functions.
A final note: You may not need the lower functions; my test database is case-sensitive, therefore the need to make the casing consistent.
I'm trying to create a text file that contains the value of a custom field I added on redmine. I tried to get it from an SQL query in the create method of the project_controller.rb (at line 80 on redmine 1.2.0) as follows :
sql = Mysql.new('localhost','root','pass','bitnami_redmine')
rq = sql.query("SELECT value
FROM custom_values
INNER JOIN projects
ON custom_values.customized_id=projects.id
WHERE custom_values.custom_field_id=7
AND projects.name='#{#project.name}'")
rq.each_hash { |h|
File.open('pleasework.txt', 'w') { |myfile|
myfile.write(h['value'])
}
}
sql.close
This works fine if I test it in a separate file (with an existing project name instead of #project.name) so it may be a syntax issue but I can't find what it is. I'd also be glad to hear any other solution to get that value.
Thanks !
(there's a very similar post here but none of the solutions actually worked)
First, you could use Project.connection.query instead of your own Mysql instance. Second, I would try to log the SQL RAILS_DEFAULT_LOGGER.info "SELECT ..." and check if it's ok... And the third, I would use identifier instead of name.
I ended up simply using params["project"]["custom_field_values"]["x"] where x is the custom field's id. I still don't know why the sql query didn't work but well, this is much simpler and faster.