Table: pages
Field: url
Issue: http://google.com//search?q=something
I have a few thousand rows where this has happened I would like a query that removes the following in bold if that is possible..
http://google.com//<--- remove the extra forward slash
http:// <--- Not to be touched
If anyone knows a MySQL query for this that would be great!
Thanks
You can use the REPLACE function. It requires three parameters
first is the source string,
second is the string to be searched for
and third is the string to replace
UPDATE pages SET url=REPLACE(url, '.com//', '.com/') WHERE url LIKE '%.com//%'
Related
I have a MySQL database on a WordPress site where I want to find all the posts in a certain table that contain a URL (within a specific subdirectory) that doesn't have a trailing slash and add one.
For example, find URLs like:
https://www.example.com/directory/test
And change them to:
https://www.example.com/directory/test/
Some already have a trailing slash, so I don't want to add// on the end of those.
The directory they are in is constant so the URL will always contain /directory/*
Any ideas on what regular expression I should use? I am using the Better Search and Replace Plugin
https://deliciousbrains.com/wp-migrate-db-pro/doc/find-and-replace/#regex-find-replace
You can try this for not capturing the string without "/" if it's present
/(?>\/$)/
I have the following route in web.php:
Route::get('posts/{encoded_id}/{slug}', 'PostController#show')
... and it works fine:
http://example.test/posts/1Dl89aRjpk/this-is-some-title
But the "problem" is that it will also work when I add a white space at the end of route parameter {encoded_id}:
http://example.test/posts/1Dl89aRjpk /this-is-some-title
// or
http://example.test/posts/1Dl89aRjpk%20 /this-is-some-title
// or
http://example.test/posts/1Dl89aRjpk%20%20 /this-is-some-title
With whitespace added at the end - this will work normally and there is no 404:
Post::where('encoded_id', $encoded_id)->firstOrFail();
... but why? And how can I make it to fail (to give 404)?
Maybe because of the type of field in the DB (CHAR)?
$table->char('encoded_id', 10)
If that's why - is there any way to configure MySQL in databases.php so that it will prevent this?
Or maybe it has something to do with .htaccess (I'm using XAMPP / Windows)?
I'm using Laravel 5.6.
EDIT:
I'm asking why this is happening and how can I prevent it, not how to trim route parameter. For example, add white space at the end of the question id on stackoverflow url and you will get 404:
https://stackoverflow.com/questions/51068436 /laravel-route-parameters-not-trimmed-it-normally-works-when-whitespace-is-added
This is due to expected SQL behaviour. In your controller you receive the full $encoded_id with spaces. All what Laravel does for you, is calling an SQL select query with WHERE. SQL ignores trailing spaces in WHERE comparison.
See this question.
If you want a 404, replace spaces in the ID to some dummy character:
$encoded_id = str_replace(' ', '#', $encoded_id);
Do this only if it is guaranteed that the ID doesn't contain spaces or hash marks otherwise.
Building on balping's answer. Some other solutions would be:
Replace all trailing spaces with #
preg_replace("/\s+$/", "#", $encoded_id);
Use trim in combination with str_pad and strlen. This will trim the whitespaces from the front and back but pad the string with #'s so it's still the original length.
str_pad(trim($encoded_id), strlen($encoded_id), '#');
Using MySQLAdmin. Moved data from Windows server and trying to replace case in urls but not finding the matches. Need slashes as I don't want to replace text in anything but the urls (in post table). I think the %20 are the problem somwhow?
UPDATE table_name SET field = replace(field, '/user%20name/', '/User%20Name/')
The actual string is more like:
https://www.example.com/forum/uploads/user%20name/GFCI%20Stds%20Rev%202006%20.pdf
In a case you are using MariaDB you have REGEXP_REPLACE() function.
But best approach is to dump the table into the file. Open it in a Notepad ++
and run regex replace like specified on a pic:
Pattern is: (https:[\/\w\s\.]+uploads/)(\w+)\%20(\w+)((\/.*)+)
Replace with: $1\u$2\%20\u$3$4
Then import the table again
Hope this help
If its MariaDB, you can do the following:
UPDATE table_name SET field = REGEXP_REPLACE(field, '\/user%20name\/', '\/User%20Name\/');
First, please check, what is actually stored in the database: %20 is a html-entity which represents a whitespace. Usually, when you are storing this inside the database, it will be represented as an actual whitespace (converted before you store it) -> Hence your replace doesn't match the actual data.
The second option that might be possible - depending on what you want to do: You are seeing the URL containing %20, therefore you created your database records (which you would like to fetch) with that additional %20 - And when you now try to query your results based on the actual url, the %20 is replaced with an "actual" whitespace (before your query) and hence it doesn't match your stored data.
I just migrated a SMF forum to WordPress BBPress. The problem is video urls in posts are not on their own line so WordPress shows the url instead of displaying the video.
I'd like to do a search and replace in the database and put in a return before any YouTube url.
Example:
What do you think???https://www.youtube.com/watch?v=SbbM_v2_5wA
Would become:
What do you think???
https://www.youtube.com/watch?v=SbbM_v2_5wA
How would I do this?
Try this on a backup instance of your data first as it may not do exactly what you expect; in short the below will replace every FIELD containing the string 'https://www.youtube.com/' with the same string cut by a newline preceeding the url. If you run the code twice on your data it WILL insert a second newline, which is probably not what you want.
update TABLENAME set FIELD = concat(substring(FIELD, 1, locate('https://www.youtube.com/', FIELD)-1),'\n',substring(FIELD, locate('https://www.youtube.com/', FIELD))) where locate('https://www.youtube.com/', FIELD) > 0;
You will need to change identifiers TABLENAME and FIELD to reflect your schema.
I have recently found a bug in my website that has been inserting an extra backslash in the URL. I have fixed the bug, but would like to clean up my historical data.
My table name is visits and column name is url. Some of the example URLs in the field are:
www.mysite.com//
www.mysite.com//restaurant/mcdonalds/
www.example.com//
How can I do a find and replace on // to change it to / in MySQL?
I would greatly appreciate any help. Thanks!
update visits set url=replace(url, '//', '/');