English 中文(简体)
用ash子替代吗?
原标题:Trying to replace underscore _ with a dash - within an href tag

I m试图用红she子,用红she子,从数据库中提取大量案文:

现有案文:

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

由于它目前位于我的奇克勒数据库,我认为,如果我能用一张纸面说明来采取行动,就会更快,但如果不可能的话,我就想利用一个S.Cregex这样做。

由于某种原因,我确实希望取代经常案文中的强调。 只有那些属于幼.的人。

最佳回答

MySQL的reg只供搜索。 他们根本不支持替换。 你们可以利用这些记录找到需要确定的记录,但随后,你仅为了实际改变记录,只能依靠在我的餐厅内进行基本的扼杀行动。

你们最好不要把相应的记录带进PHP,并在那里进行改动。 当然,哪一种做法在html上引起了使用。 而实际操作则使用购买力平价。

问题回答

You can do it with 1 update sql query. 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.

If you have multiple a href links in one field. 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.

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

- ——在您的所有更新工作完成之后,从小链条链条中清除星体。

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

<>-> 页: 1

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




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签