Skip to content

Instantly share code, notes, and snippets.

@pandanote-info
Created December 3, 2018 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pandanote-info/7a3da23821702b46514d1175b546300e to your computer and use it in GitHub Desktop.
Save pandanote-info/7a3da23821702b46514d1175b546300e to your computer and use it in GitHub Desktop.
MariaDBに格納されているimgタグをいったんaタグで囲みつつ、もし二重にaタグで囲んでしまった場合には一組のタグを残して削除するためのテスト用のSQL文。
MariaDB [(none)]> select regexp_replace(
regexp_replace('<img src="https://pandanote.info/wordpress/wp-content/uploads/2018/12/P_20181201_191300_vHDR_Auto_a-233x300.jpg" alt="" width="233" height="300" class="alignnone size-medium wp-image-3517" />',
'(<img src=.*wp-image-(\\d+)[^>]*>)',
'<a href="https://pandanote.info/?attachment_id=\\2" rel="attachment wp-att-\\2">\\1</a>'),
'(?:(<a href[^>]*>))+(<img[^>]*>)(</a>)+',
'\\1\\2\\3');
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| regexp_replace(regexp_replace('<img src="https://pandanote.info/wordpress/wp-content/uploads/2018/12/P_20181201_191300_vHDR_Auto_a-233x300.jpg" alt="" width="233" height="300" class="alignnone size-medium wp-image-3517" />','(<img src=.*wp-image-(\\d+)[^>] |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| <a href="https://pandanote.info/?attachment_id=3517" rel="attachment wp-att-3517"><img src="https://pandanote.info/wordpress/wp-content/uploads/2018/12/P_20181201_191300_vHDR_Auto_a-233x300.jpg" alt="" width="233" height="300" class="alignnone size-medium wp-image-3517" /></a> |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment