MySQL 在 phpMyAdmin 中查找和替换(删除)正则表达式

MySQL Find and Replace (Delete) Regex in phpMyAdmin

我正在尝试对 MySQL 数据库(大约 20k+ 条记录)中的特定列执行查找和替换(实际上是删除),其中相关列包含 HTML( CMS 中页面内容的正文)。

我需要从每条记录中找到并 replace/delete 以下文本,并保持所有其他 HTML 完整(以下字符串每条记录只出现一次):

<h2>Location Map</h2><p><a href="[?]" onclick="window.open(this.href); return false;">[?]</a></p>

其中“[?]”是一个可变内容和长度的字符串(包括字母数字和符号),其余静态内容完全如上所示。

我知道这应该是一个非常简单且有点愚蠢的问题,但我不是正则表达式专家,无法完全弄清楚我做错了什么(可能没有转义足够的字符或太多) .

最好我想要一个可以在 phpMyAdmin 中使用的正则表达式来执行查找和替换。

非常感谢。

PHP 中的 find/replace 语句可能是这样的:

$regex = '#<h2>Location Map</h2><p><a href="(.*?)" onclick="window.open.*?>(.*?)</a></p>#';
$result = preg_replace($regex, $replace, $html);

...其中 $html 是您从数据库中检索的原始数据,而 $replace要插入的值而不是匹配的字符串。

例如,如果:

$html = 'all that comes before
<h2>Location Map</h2><p><a href="http://www.foofle.com" onclick="window.open(this.href); return false;">Foofle</a></p>
all that comes after';

$replace = "(link=, text=)";

$result = preg_replace($regex, $replace, $html);
echo $result;

输出:

all that comes before
(link=http://www.foofle.com, text=Foofle)
all that comes after