2017-04-12 60 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