开发者

MySQL: How can I remove trailing HTML from a field in the database?

开发者 https://www.devze.com 2023-01-15 06:25 出处:网络
I want to remove some rogue HTML from a DB field that is supposed to contain a simple filename. Example of ok field:

I want to remove some rogue HTML from a DB field that is supposed to contain a simple filename. Example of ok field:

myfile.pdf

Example of not ok field:

myfile2.pdf<input type="hidden" id="gwProxy" />...

Does anyone know a query I can run that can remove the HTML part but leave the filename? i.e. remove everything from the first < character onwards.

Lets assume the field is called myattachment and is defined as a varchar(250) and the table is called mytable in a MySQL database.


Background info (not necessary to read):

The field in our da开发者_运维问答tabase is supposed to contain filenames however, due to a issue (documented here) some of the fields now contain a filename and some rogue HTML. We have fixed the root issue and now need to fix the corrupt fields. In the past I have replaced text using this kind of query:

UPDATE mytable SET myattachment = replace(myattachment, 'JPG', 'jpg') WHERE myattachment LIKE '%JPG';


This query seems to work ok, can anyone see any issues with it?

UPDATE mytable
   SET myattachment = SUBSTRING_INDEX(myattachment, '<', 1) 
 WHERE `myattachment` LIKE '%<%';

For docs on SUBSTRING_INDEX see the mysql manual page.

0

精彩评论

暂无评论...
验证码 换一张
取 消