简体   繁体   English

MySQL搜索和替换(相似文本-不准确)

[英]MySQL Search and Replace (Similar Text - Not Exact)

I want to replace this: 我要替换为:

~ <a href="xxxx">Download</a>

with: 与:

<div class="down"> ~ <a href="xxxx">Download</a> </div>

There are around 300-350 places the DOWNLOAD word is repeated. 大约有300-350个地方会重复出现“ DOWNLOAD字。 xxx is not constant, it changes every time, and the hyper link will be changed every time... xxx不是常数,它每次都会更改,并且超级链接也会每次更改...

Is it possible to do that with any SQL Query? 是否可以使用任何SQL查询来做到这一点? If it's not possible in SQL, can this be done in php? 如果在SQL中不可能,可以在php中完成吗?

How about: 怎么样:

UPDATE MyTable 
SET MyField = REPLACE(MyField, 
                      '~ <a href="xxxx">Download</a>', 
                      '<div class="down"> ~ <a href="xxxx">Download</a> </div>')

Ok, how about rplacing everything around the "xxxx" : 好的,如何替换"xxxx"周围的所有内容:

UPDATE MyTable 
SET MyField = 
REPLACE(
  REPLACE(MyField, 
          '~ <a href=', 
          '<div class="down"> ~ <a href'),  
  '</a>', 
  '</a> </div>')

What about a PHP method using str_replace() 那么使用str_replace()的PHP方法呢?

<?php

$string = '~ <a href="xxxx">Download</a>';

$string = str_replace('~ <a href="xxxx">Download</a>', '<div class="down"> ~ <a href="xxxx">Download</a> </div>', $string);

echo $string;

Echo's the following in HTML source as: 在HTML源代码中,Echo的以下内容为:

<div class="down"> ~ <a href="xxxx">Download</a> </div>

Additionally: 另外:

$link = "page.php";

$string = '~ <a href="xxxx">Download</a>';

$string = str_replace('~ <a href="xxxx">Download</a>', '<div class="down"> ~ <a href="'.$link.'">Download</a></div>', $string);

echo $string;

HTML source: HTML来源:

<div class="down"> ~ <a href="page.php">Download</a></div>

Get the rows from table and use something like following. 从表中获取行,并使用类似以下的内容。

foreach($result as $row)
{
   echo '<div class-="down"><a href = "'. $row['xxxx'] .'" >Download</a></div>';
}

MySQL has a REGEXP operator, but that works only for "matching", not for doing replacements. MySQL具有REGEXP运算符,但仅适用于“匹配”,不适用于替代。

An expression like this: 这样的表达式:

REPLACE(
  REPLACE(col, 
    , '~ <a href="'
    , '<div class="down"> ~ <a href="'
  )
  , '">Download</a>'
  , ">Download</a> </div>'
)

might achieve the results you are after but the two replacements will be done independently. 可能会达到您想要的结果, 但是两个替换将独立完成。 That is, all instances of '~ <a href="' within the string, not just those instances that are followed by Download . 也就是说,字符串中'~ <a href="' 所有实例,而不仅仅是Download之后的实例。

I strongly suspect that this won't really work for you, because I suspect that the column you need to perform this operation on contains other instances of these same strings, which do not need to be replaced. 我强烈怀疑这对您确实不起作用,因为我怀疑您需要对之执行此操作的列包含这些相同字符串的其他实例,无需替换。


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

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