用通配符替换字符串

时间:2017-04-12 16:51:36

标签: mysql database wordpress replace

我正在将我从自定义CMS管理的网站移动到Wordpress,并且发现图像标记上的某些属性在WP环境中显示有问题。要解决此问题,我需要删除内联到post_content表的wp_posts列中每个图像标记的height属性。

从数据库中的原始值开始,我需要以下内容:

<img src="http://example.com/img/20150823_image.jpg" style="width: 730px; height: 730px;" />

成为:

<img src="http://example.com/img/20150823_image.jpg" style="width: 730px;" />

所以,基本上,我需要删除&#34;身高:730px;&#34;一部分。它是图像特定的,所以在这种情况下它是730但在另一个它可能是1500,447,80等。

我试图看看我是否可以使用&#39;%&#39;作为一个通配符,但似乎没有工作......

UPDATE wp_posts SET post_content = REPLACE(post_content,' height: %px;','');

任何帮助都会非常感激,因为我不需要手动浏览数千行来删除它们。

3 个答案:

答案 0 :(得分:2)

您可以使用函数进行文本解析:

create function f_strip_height( in_s text ) returns text
begin

declare v_start int;
declare v_end int;
declare v_height text;

select locate(' height:', in_s ) into v_start;
if (v_start>0) then

  select locate( 'px;', substring( in_s, v_start)  ) into v_end;

  select trim(substring(substring( in_s, v_start, v_end+2), 9)) into v_height;

  if (v_end>0 and concat(cast(v_height as unsigned), 'px;' =  v_height)) then
    return concat(substring( in_s, 1, v_start-1), substring( in_s, v_start+v_end+2));
  end if;
end if;

return in_s;
end

然后使用函数:

UPDATE wp_posts SET post_content = f_strip_height(post_content);

答案 1 :(得分:1)

这不是SQL的工作。这是一个简单的(?)PHP脚本应该可以解决这个问题,尽管我这样做是不可能的,所以不能保证:

<?php
// create the DB connection
$db = new PDO("mysql:host=localhost;dbname=wordpress", "user", "password");
// quiet warnings
libxml_use_internal_errors(true);
// prepare the update statement for later
$stmt = $db->prepare("UPDATE wp_posts SET post_content = ? WHERE post_id = ?");
// select the posts that at least have the word "height:" in them
$posts = $db->query("SELECT post_id, post_content FROM wp_posts WHERE post_content LIKE '%height:%'");
// loop through the posts
while ($post = $posts->fetch(PDO::FETCH_ASSOC)) {
    // create a DOM document
    $dom = new DomDocument();
    // load the HTML into the DOM parser
    $dom->loadHTML($post["post_content"], LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    // prepare the XPath
    $xpath = new DomXPath($dom);
    // get all img elements with a style attribute containing the word height
    $imgs = $xpath->query("//img[contains(@style, 'height')]");
    foreach ($imgs as $img) {
        // get the style attribute value
        $style = $img->getAttribute("style");
        // remove height
        $style = preg_replace("/height\s*:\s*\d+(px)?;?/", "", $style);
        // replace the attribute value
        $img->setAttribute("style", $style);
    }
    // output the new HTML
    $newhtml = $dom->saveHTML();
    echo "Updating post $post["post_id"] with new content:\n$newhtml\n\n";
    // save it into the database -- uncomment this line when you trust the script!
//    $stmt->execute([$newhtml, $post["post_id"]]);
}

答案 2 :(得分:1)

如果您拥有相应的权限,则可以使用UDF 27.4.2 Adding a New User-Defined Function,其中一些可以是:

在另一种情况下,如前所述,您可以执行自己的功能,这里有一个可以根据需要修改和调整的版本:

mysql> DROP TABLE IF EXISTS `wp_posts`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `wp_posts` (
    ->     `post_content` TEXT
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO `wp_posts`
    ->     (`post_content`)
    -> VALUES
    ->     ('<img src="http://example.com/img/20150823_image.jpg" style="width: 730px; height: 730px;" />'),
    ->     ('<img src="http://example.com/img/20150824_image.jpg" style="width: 730px; height: 1500px;" />'),
    ->     ('<img src="http://example.com/img/20150825_image.jpg" style="width: 730px; height: 80px;" />'),
    ->     ('<img src="http://example.com/img/20150826_image.jpg" style="width: 730px; height: 0px;" />'),
    ->     ('<img src="http://example.com/img/20150827_image.jpg" style="width: 730px;" />');
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> DELIMITER //

mysql> DROP FUNCTION IF EXISTS `get_string`//
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE FUNCTION `get_string`(`_string` TEXT,
    ->                              `_begin` VARCHAR(255),
    ->                              `_end` VARCHAR(255))
    ->     RETURNS TEXT DETERMINISTIC
    -> BEGIN
    ->     DECLARE `_begin_pos` INT UNSIGNED DEFAULT LOCATE(`_begin`, `_string`);
    ->     DECLARE `_end_pos` INT UNSIGNED DEFAULT 0;
    ->     IF `_begin_pos` IS NOT NULL AND `_begin_pos` > 0 THEN
    ->         SET `_end_pos` := LOCATE(`_end`, `_string`, `_begin_pos`);
    ->         IF `_end_pos` IS NOT NULL AND `_end_pos` > 0 THEN
    ->             RETURN SUBSTRING(`_string`,
    ->                              `_begin_pos`,
    ->                              (`_end_pos` + CHAR_LENGTH(`_end`)) - `_begin_pos`); 
    ->         END IF;
    ->     END IF;
    ->     RETURN '';
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> SELECT `post_content`
    -> FROM `wp_posts`;
+-----------------------------------------------------------------------------------------------+
| post_content                                                                                  |
+-----------------------------------------------------------------------------------------------+
| <img src="http://example.com/img/20150823_image.jpg" style="width: 730px; height: 730px;" />  |
| <img src="http://example.com/img/20150824_image.jpg" style="width: 730px; height: 1500px;" /> |
| <img src="http://example.com/img/20150825_image.jpg" style="width: 730px; height: 80px;" />   |
| <img src="http://example.com/img/20150826_image.jpg" style="width: 730px; height: 0px;" />    |
| <img src="http://example.com/img/20150827_image.jpg" style="width: 730px;" />                 |
+-----------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)

mysql> UPDATE `wp_posts`
    -> SET `post_content` = REPLACE(`post_content`, `get_string`(`post_content`, ' height:', ';'), '');
Query OK, 4 rows affected (0.01 sec)
Rows matched: 5  Changed: 4  Warnings: 0

mysql> SELECT `post_content`
    -> FROM `wp_posts`;
+-------------------------------------------------------------------------------+
| post_content                                                                  |
+-------------------------------------------------------------------------------+
| <img src="http://example.com/img/20150823_image.jpg" style="width: 730px;" /> |
| <img src="http://example.com/img/20150824_image.jpg" style="width: 730px;" /> |
| <img src="http://example.com/img/20150825_image.jpg" style="width: 730px;" /> |
| <img src="http://example.com/img/20150826_image.jpg" style="width: 730px;" /> |
| <img src="http://example.com/img/20150827_image.jpg" style="width: 730px;" /> |
+-------------------------------------------------------------------------------+
5 rows in set (0.00 sec)

Rextester中的示例。