簡體   English   中英

嘗試在href標記內將破折號_替換為破折號

[英]Trying to replace underscore _ with a dash - within an href tag

我正在嘗試使用href屬性中的破折號替換下划線,該href屬性是來自數據庫的大量文本:

現有文字:

Hello, my name is <a href="http://example.com/joe_smith">joe smith</a> and I  
eat pizza with my friend <a href="http://example.com/john_doe">john doe</a>.

輸出:

Hello, my name is <a href="http://example.com/joe-smith">joe smith</a> and I 
eat pizza with my friend <a href="http://example.com/john-doe">john doe</a>.

因為它當前在mysql數據庫中,所以我認為如果可以使用sql語句執行操作會更快,但是如果不可能,我想使用php正則表達式來完成。

我不想替換由於某種原因而在常規文本中出現的下划線。 僅href中的內容。

MySQL的正則表達式僅用於搜索。 他們根本不支持更換。 您可以使用它們來查找需要修復的記錄,但是只有實際更改記錄時,才限於mysql中的基本字符串操作。

您最好將匹配的記錄拉入PHP並在那里進行更改。 然后,哪個當然可以在html上使用正則表達式……請不要這樣做。 使用PHP的DOM代替實際操作。

您可以使用1個更新sql查詢來完成。 我為您准備了一個測試表,並更新了查詢以進行演示。 基本上是在您自己的表上使用,只需將表名從TestTable更改為表名,然后將“ Field”的名稱更改為要更新的字段名。

如果您在一個字段中有多個href鏈接。 您需要多次執行查詢。 您可以通過第一個查詢在表中找到最大鏈接出現次數。 比執行更新查詢多次。 當您更新查詢的次數是多於出現1的查詢時,我為您提供了一些清除我使用的臨時數據的查詢。

-查找表中最大的鏈接出現次數

SELECT max(cast((LENGTH(Field) - LENGTH(REPLACE(Field, '<a href', ''))) / 7 as unsigned)) AS occurrence_count 
FROM TestTable;

-更新您的表格出現次數以替換所有href鏈接。

update TestTable 
set Field = replace
                (
                   @b:=replace
                   (
                     @a:=replace(Field
                      , substring(Field, Instr(Field, "<a href='"), Instr(Field, "</a>")-Instr(Field, "<a href='")+4)
                      , replace(substring(Field, Instr(Field, "<a href='"), Instr(Field, "</a>")-Instr(Field, "<a href='")+4), "_", "-")
                      )
                     , substring(@a, Instr(@a, "<a href='"), Instr(@a, "</a>")-Instr(@a, "<a href='")+4)
                     , replace(substring(@a, Instr(@a, "<a href='"), Instr(@a, "</a>")-Instr(@a, "<a href='")+4), "<a href=", "<*a href=")
                   )
                 , substring(@b, Instr(@b, "<*a href='"), Instr(@b, "</a>")-Instr(@b, "<*a href='")+4)
                 , replace(substring(@b, Instr(@b, "<*a href='"), Instr(@b, "</a>")-Instr(@b, "<*a href='")+4), "</a>", "</*a>")
                )
;

-當所有更新完成后,只需運行一次即可清除href鏈接中的星標。

update TestTable set Field = replace(replace(Field, "<*a href", "<a href"), "</*a>", "</a>")

-檢查你的桌子

select * from TestTable;

試驗台

CREATE TABLE `testtable` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `Field` VARCHAR(255) NOT NULL DEFAULT '',
    PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM
ROW_FORMAT=DEFAULT

測試數據

Insert into TestTable (Field) values ("Hello, my name is <a href='http://example.com/joe_smith'>joe smith</a> and I eat pizza with my friend <a href='http://example.com/john_doe'>john doe</a>");
Insert into TestTable (Field) values ("Hello, my name is <a href='http://example.com/joe_smith'>joe smith</a> and I eat pizza with my friend <a href='http://example.com/john_doe'>john doe</a> my friend <a href='http://example.com/john_doe'>jane doe</a>");
(<a href=".+?)_(.+?">)

有更換

$1-$2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM