简体   繁体   English

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

[英]MySQL Find and Replace (Delete) Regex in phpMyAdmin

I'm trying to perform a find and replace (actually, a delete) on a particular column in a MySQL database (circa 20k+ records) where the column in question contains HTML (the body text for page content in a CMS).我正在尝试对 MySQL 数据库中的特定列(大约 20k+ 记录)执行查找和替换(实际上是删除),其中相关列包含 HTML(CMS 中页面内容的正文)。

I need to find and replace/delete the following text from within each record and leave all of the other HTML intact (the below string appears only once per record):我需要在每条记录中查找并替换/删除以下文本,并使所有其他 HTML 保持原样(以下字符串每条记录仅出现一次):

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

Where "[?]" is a string of variable content and length (both alphanumeric and symbols) and the rest static content exactly as it's shown above.其中“[?]”是一串可变内容和长度(字母数字和符号),其余静态内容与上面显示的完全一样。

I know this should be very simple and a bit of a stupid question but I'm no regular expression expert and can't quite work out what I'm doing wrong (possible not escaping enough characters or too many).我知道这应该很简单,而且是一个有点愚蠢的问题,但我不是正则表达式专家,无法弄清楚我做错了什么(可能没有转义足够多的字符或太多)。

Preferably I'd like a regex I can use in phpMyAdmin to perform the find and replace.最好我想要一个可以在 phpMyAdmin 中使用的正则表达式来执行查找和替换。

Many thanks in advance.提前谢谢了。

The find/replace statement in PHP could be something like this: PHP 中的 find/replace 语句可能是这样的:

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

...where $html would be the original data you retrieve from your database, and $replace the value you want to insert instead of the matched string. ...其中$html将是您从数据库中检索的原始数据,并$replace您要插入的值而不是匹配的字符串。

For instance, if:例如,如果:

$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=$1, text=$2)";

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

Outputs:输出:

all that comes before之前的一切
(link= http://www.foofle.com , text=Foofle) (链接= http://www.foofle.com ,文本=傻瓜)
all that comes after之后的一切

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM