Replacing beginning and end of multiple URLs in mysql - mysql

I'm in the process of converting my wordpress website to https, and the issue I have is that a bunch of pages have external embeds that require a different url for https.
So I need to replace:
http://online.anyflip.com/code1/code2/
with:
https://s3.amazonaws.com/code1/code2/index.html
where code1 and code2 are different for each url.
So something like:
UPDATE wpe_posts
SET post_content = ( Replace (post_content, 'src="http://online.anyflip.com/$code1$/$code2$/', 'src="https://s3.amazonaws.com/online.anyflip.com/$code1$/$code2$/index.html') )
That isn't this obviously wrong.
Any help would be much appreciated.

Try this,
Instead of replacing Entire String just replace the part after src,
SET post_content = ( Replace (post_content, 'http://online.anyflip.com/$code1$/$code2$/', 'https://s3.amazonaws.com/online.anyflip.com/$code1$/$code2$/index.html') )

Related

MySQL find and replace a string with variable content

For some reasons, I uninstalled VC Bakery plugin on WordPress, but i have like 6000 posts using VC short codes, I'm trying to replace those shortcodes with something else. For e.g.
What SQL query can I use to turn this content:
[vc_row][vc_column][vc_single_image image="48526" img_size="full"][/vc_column][/vc_row]
To:
<img src="somethinghere" alt=""/>
Applying changes to table "wp_posts", column "post_content".
Any solution ? thanks :)
Why not just use string function replace()?
uppdate wp_posts
set post_content = replace(
post_content,
'[vc_row][vc_column][vc_single_image image="48526" img_size="full"][/vc_column][/vc_row]',
'<img src="somethinghere" alt=""/>'
)
where post_content like '%[vc_row][vc_column][vc_single_image image="48526" img_size="full"][/vc_column][/vc_row]%'

Replacing a string that start with and end with in phpmyadmin

I'm trying to update a particular url in wordpress post_content table. I have over 5000 differents urls to update for a single same one.
So in clear I have 5000 urls that all starts with :
domain.com/params.php?....
AND always ends with :
?e=
I want all those url to be replace with a single one without parameters in so they will all send to domain.com/page.html
So is there a way to replace all the urls without having to manually doing it ?
Thanks
Is this what you are looking for?
update post_content
set url = 'domain.com/page.html'
where url like 'domain.com/params?%?e=0'
This phrases as : replace all urls that start with 'domain.com/params?' and end with '?e=0' with 'domain.com/page.html'.
No it is not working. It's being written as it :
Update `wp_posts`
set post_content = 'domaine.com/newpage.php'
where post_content like 'domaine.com/ads.php?city=%e='
so it should take all the queries starting with domaine.com/ads.php?city= until e= but it is not and returning 0 value.
Some knows what is wrong ?

MySQL: Why aren't url's matching when using REPLACE?

My Situation:
I have url's in a field containing blog posts. The url's are being stored in my database with escape characters. My task at the moment is to replace some already inserted 'http' url's with 'https' url's, but REPLACE will match neither the original url nor the escaped url. I can't just replace every instance of 'http:', because I only want to affect certain links in each post, not every link.
I am very familiar with SQL, as well as REPLACE, so I'm not just asking how REPLACE works and how to use it. Another user here has tested my queries in his environment and they work. So, there must be something in my configuration that is preventing the queries from functioning as expected.
I have searched this site and google extensively for several hours and have found nothing specifically addressing my issue. Everything I have tried is included below and if there is something else I should try, I don't know what that is and I haven't found any suggestions/posts/comments that suggest doing anything differently.
Example URL:
http://test01.mysite.com
As Stored in DB:
http:\/\/test01.mysite.com
Code to Re-Create Situation:
DROP TABLE IF EXISTS test_posts;
CREATE TABLE IF NOT EXISTS test_posts (
id int NOT NULL AUTO_INCREMENT,
post_content longtext NOT NULL,
PRIMARY KEY (id)
)
INSERT INTO
test_posts
(post_content)
VALUES
('content content content Link I want to change content content content Link I don\'t want to change content content content Link I want to change content content content Link I don\'t want to change');
If I run
UPDATE
test_posts
SET
post_content = REPLACE(post_content, 'http://test01.mysite.com', 'https://test01.mysite.com');
or
UPDATE
test_posts
SET
post_content = REPLACE(post_content, 'http:\/\/test01.mysite.com', 'https://test01.mysite.com');
zero records are affected.
For testing purposes, I ran the following query which returns 0 rows.
SELECT
*
FROM
test_posts
WHERE
post_content LIKE '%http://test01.mysite.com%'
OR
post_content LIKE '%http:\/\/test01.mysite.com%'
OR
post_content LIKE '%http:\\/\\/test01.mysite.com%'
OR
post_content LIKE 'http:%/%/test01.mysite.com%';
If I run:
SELECT
*
FROM
test_posts
WHERE
post_content LIKE '%http:_/_/test01.mysite.com%'
It does return matches, but that doesn't solve the real problem of how to match when using UPDATE/REPLACE.
I have tried on two different servers and I get the same results on both.
I have tried the following Engine/Collation combinations and all return the same 0 records results:
MyISAM/latin1_swedish_ci
MyISAM/utf8mb4_unicode_ci
InnoDB/latin1_swedish_ci
InnoDB/utf8mb4_unicode_ci
Anybody know how I can write these queries so that REPLACE will find matches to those url's or what settings in my database or PhpMyAdmin may be causing the queries to return/affect 0 rows?
I think the backslash must be escaped in MySQL
field_name LIKE 'http:\\/\\/test01.mysite.com%'
Of course one could go for sure and use the single char wildcard __
field_name LIKE 'http:_/_/test01.mysite.com%'
or for your both cases: an optional backslash:
field_name LIKE 'http:%/%/test01.mysite.com%'
I'm still baffled as to why the queries with LIKE won't work, but, sadly, using those to narrow down the problem clouded my judgement and I didn't try all the same combinations in the REPLACE functions.
The following works:
UPDATE
test_posts
SET
post_content = REPLACE(post_content, 'http:\\/\\/test01.mysite.com', 'https://test01.mysite.com');
If anyone can explain to me why these combinations work with REPLACE, but not with LIKE, I'd really love to know. Thanks!
There is no reason, your query won't work if you have run properly, there is something else, you may be missing here.
UPDATE
test1
SET
name_1 = REPLACE(name_1, 'http:\/\/test01.mysite.com', 'https://test01.mysite.com')
works well and does the job of repalcing the \/ with /.
See screen-shot attached,
You may have some other problem, please check and update the question, if so.
Edit after comments
If you have more data points in URL, change query like below.
UPDATE
test1
SET
name_1 = REPLACE(name_1, '\/', '/')
Above will replace all the occurrence of \/ with /.
As \\ did not work to represent/escape a backslash, use regular expression functions:
REGEXP_LIKE('.*http:\\/\\/test01\.mysite.com.*')
REGEXP_REPLACE(field, 'http:\\/\\/', 'http://')
Here \\ should work.

How to get post slug from Drupal database

I'm trying to export Drupal posts over to Wordpress (which is in itself a hassle). I can't figure out how to maintain the URLs of the blog posts though. Some of them are customized:
Blog titled Story of Soil is blog/2012/03/03/soil-story in Drupal. One titled Welcome John Doe is simply /john
Is there a Drupal function to making these URLs? Where does it store the customized blog posts?
You can get the URL Alias by using the url method.
$url = url('node/' . $nid);
You should be able to get the Alias for a node by using drupal_lookup_path:
// alias: return an alias for a given Drupal system path (if one exists).
$alias = drupal_lookup_path('alias', $node->nid);
Drupal manual: drupal_lookup_path or the reverse, look up node/internal path from alias: drupal_get_normal_path.
It seems the url function function that Rawkode posted does about the same, so I guess it comes down to your personal preference.
Also see: http://daipratt.co.uk/how-to-get-the-path-of-a-node-from-the-node-id-in-drupal/
I found the above tremendously helpful because i was hoping to get what I needed from the db when I was looking and not by using a function. it gave me a good starting point but running this as it sits does nothing but error out because those are not the right column names. Keep in mind this updates stuff in place and will break everything if you are not working on a copy of the database. This code will fix it:
UPDATE `url_alias`
SET source=SUBSTRING_INDEX(`source`, '/', -1),
alias=SUBSTRING_INDEX(`alias`, '/', -1)
WHERE 1
since im not big on doing things in place I just added two columns nid and slug and then ran this query
UPDATE `url_alias`
SET nid=SUBSTRING_INDEX(`source`, '/', -1),
slug=SUBSTRING_INDEX(`alias`, '/', -1)
WHERE 1
I found the url_alias table in the Drupal database, and I ran this SQL statement:
UPDATE `url_alias`
SET src=SUBSTRING_INDEX(`src`, '/', -1),
dst=SUBSTRING_INDEX(`dst`, '/', -1)
WHERE 1
src is now the nid and dst is now the slug. I can rename them and then INSERT INTO wp_posts as post_name

Mysql Update Field Contents

I am currently trying to edit my db named boh. The current table "files" has a field called "path". Inside the path field is an actualpath to files listed in a folder, syntax "F:\xxx\xxx\xxx\filename.xxx". How do I update the field information to replace the "F:\xxx\xxx\xxx" so that just the file name exists?
It depends what you exactly want, if you want to strip constant path you can use:
UPDATE `table` SET `path` = REPLACE(`path`, 'F:\\xxx\\xxx\\xxx', '');
If you want to keep only last part after last \, then following command should do it:
UPDATE `table` SET `path` = SUBSTRING_INDEX(`path`. '\\', -1);
did you read this?
http://dev.mysql.com/doc/refman/5.1/en/replace.html
UPDATE files
SET path = REPLACE(path, 'F:\xxx\xxx\xxx\', '')
WHERE path LIKE = 'F:\xxx\xxx\xxx\%'
It's very easy to ruin your data with this massive updates so make sure you:
Try it first with a SELECT sentence
Backup your data
Assuming 'F:\xxx\xxx\xxx\' is not constant you could try a statement like this one:
UPDATE files SET path = REVERSE(SUBSTR(REVERSE(path), 1, LOCATE(REVERSE(path), '\')));