2017-04-12 63 views
2

我正在将自定义CMS管理的网站移动到Wordpress,并发现图像标记上的一些属性在WP环境中显示一次时会出现问题。为了解决这个问题,我需要去除wp_postspost_content列中每个图片标签内联的高度属性。用通配符替换字符串

与DB中的原始值开始,我想以下几点:

<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;" /> 

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

我试图看看我是否可以使用'%'作为通配符,但它不会似乎没有工作...

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

任何帮助将不胜感激,因为我宁愿不必手动通过成千上万的行剥离出这些。

+2

你将需要使用正则表达式来进行替换。不幸的是,SQL不提供替换功能。你最好的选择是编写一个php脚本来遍历行并进行替换。 –

+0

以下是关于LIKE的一些文档,或许它会有所帮助。 https://docs.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql –

+0

提供的答案是否有帮助?你应该**赞成** *所有有用的答案*,并且**标记接受**最好回答你的问题的答案*。这会将问题标记为“封闭”,并在网站上给您一些声誉。请参阅https://stackoverflow.com/help/someone-answers – miken32

回答

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); 
+0

只要格式完全相同('style'是元素的最后一个属性,'height'是该属性中的最后一个规则,总是使用双引号,元素自闭等。) – miken32

+0

可以很容易地改进功能,使其更通用。修改了示例代码。 – slaakso

+0

这并不是我的观点,因为我可以继续玩这个游戏:'height:0;'如果这是最后一条规则并且没有分号怎么办,那么其他有高度设置的元素呢?我想说的是,只用子字符串索引解析标记会非常脆弱,甚至比[用正则表达式解析标记](http://stackoverflow.com/q/1732348/1255289)! – miken32

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"]]); 
} 
1

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

在另一种情况下,如已经提到的,你可以做你的流n功能,此版本根据需要,你可以修改和调整:在Rextester

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) 

例。

+0

这可能会更有帮助,如果它在一个可以复制/粘贴的形式。 – miken32

+0

@ miken32:新增示例。 – wchiquito