简体   繁体   English

尝试在href标记内将破折号_替换为破折号

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

I'm trying to replace underscores with dashes within an href attribute that is in a large amount of text coming from a database: 我正在尝试使用href属性中的破折号替换下划线,该href属性是来自数据库的大量文本:

Existing Text: 现有文字:

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>.

Output: 输出:

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>.

Since it's currently in a mysql database, I assume it would be faster if I could perform the action using an sql statement, but if that's not possible I would like to do it using a php regex. 因为它当前在mysql数据库中,所以我认为如果可以使用sql语句执行操作会更快,但是如果不可能,我想使用php正则表达式来完成。

I do NOT want to replace underscores that are in the regular text for one reason or another. 我不想替换由于某种原因而在常规文本中出现的下划线。 Only the ones that are within the href. 仅href中的内容。

MySQL's regexes are for searching only. MySQL的正则表达式仅用于搜索。 They do not support replacement at all. 他们根本不支持更换。 You can use them to find the records that need fixing, but then you're limited to basic string operations within mysql only for actually changing the records. 您可以使用它们来查找需要修复的记录,但是只有实际更改记录时,才限于mysql中的基本字符串操作。

You'd be better off pulling the matched records into PHP and doing the changes there. 您最好将匹配的记录拉入PHP并在那里进行更改。 Which of course then brings up the use of regexes on html... don't do it. 然后,哪个当然可以在html上使用正则表达式……请不要这样做。 Use PHP's DOM instead for the actual manipulations. 使用PHP的DOM代替实际操作。

You can do it with 1 update sql query. 您可以使用1个更新sql查询来完成。 I prepared you a test table, and update query to demonstrate. 我为您准备了一个测试表,并更新了查询以进行演示。 Basically to use on your own table, JUST change table name from TestTable to your table's name and change the name of "Field" to your fields name which you want to update. 基本上是在您自己的表上使用,只需将表名从TestTable更改为表名,然后将“ Field”的名称更改为要更新的字段名。

If you have multiple a href links in one field. 如果您在一个字段中有多个href链接。 You need to execute query multiple times. 您需要多次执行查询。 You can find maximum link occurences in your table with first query. 您可以通过第一个查询在表中找到最大链接出现次数。 Than execute update query multiple times. 比执行更新查询多次。 When you update your query at the count of occurence_count than update 1 more query I gave you for clearing some temp data I used. 当您更新查询的次数是多于出现1的查询时,我为您提供了一些清除我使用的临时数据的查询。

-- find maximum link occurences in your table -查找表中最大的链接出现次数

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

-- Update your Table occurrence_count times to replace all a href links. -更新您的表格出现次数以替换所有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>")
                )
;

-- run this once when all your updates finishes to clear stars from a href links. -当所有更新完成后,只需运行一次即可清除href链接中的星标。

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

-- check your table -检查你的桌子

select * from TestTable;

TEST TABLE 试验台

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

TEST DATA 测试数据

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