레코드에서 HTML 태그 제거
아래 내용을 포함하는 표 1열에서 MYSQL 쿼리를 구성하는 데 도움이 필요합니다.
Row1 : this is first <a href='mytext.txt'>row</a> from the table
Row 2 : THis is the second row <img src ='mytext.jpg'> my image is there
Row 3 : <p>This is the Third row my mytext is there </p>
Row 4 : <p class='te_mytext'>This is the Third row my text is there </p>
이것은 테이블 행입니다. 키워드를 'mytext'로 검색하려고 합니다.
저의 질문은
SELECT * from table WHERE colmn_name ` like '%mytext%' "
결과적으로 4열을 다 받겠지만 결과가 틀렸습니다.저는 3열로만 정확한 출력을 받아야 합니다.이 행이 내용 내부에 있는 내 텍스트만 다른 모든 행에 있는 이유는 내용에 있지 않고 내 텍스트가 모든 행에 있는 것입니다.
MySQL 쿼리를 작성하려면 어떻게 해야 합니까?
이 해결책을 시도해 보십시오. 직접 시도해 보지는 않았지만 분명히 효과가 있습니다.
출처 : http://forums.mysql.com/read.php?52,177343,177985#msg-177985
SET GLOBAL log_bin_trust_function_creators=1;
DROP FUNCTION IF EXISTS fnStripTags;
DELIMITER |
CREATE FUNCTION fnStripTags( Dirty varchar(4000) )
RETURNS varchar(4000)
DETERMINISTIC
BEGIN
DECLARE iStart, iEnd, iLength int;
WHILE Locate( '<', Dirty ) > 0 And Locate( '>', Dirty, Locate( '<', Dirty )) > 0 DO
BEGIN
SET iStart = Locate( '<', Dirty ), iEnd = Locate( '>', Dirty, Locate('<', Dirty ));
SET iLength = ( iEnd - iStart) + 1;
IF iLength > 0 THEN
BEGIN
SET Dirty = Insert( Dirty, iStart, iLength, '');
END;
END IF;
END;
END WHILE;
RETURN Dirty;
END;
|
DELIMITER ;
SELECT fnStripTags('this <html>is <b>a test</b>, nothing more</html>');
strip_tags 함수를 구현한 내용은 다음과 같습니다.
CREATE FUNCTION `strip_tags`($str text) RETURNS text
BEGIN
DECLARE $start, $end INT DEFAULT 1;
LOOP
SET $start = LOCATE("<", $str, $start);
IF (!$start) THEN RETURN $str; END IF;
SET $end = LOCATE(">", $str, $start);
IF (!$end) THEN SET $end = $start; END IF;
SET $str = INSERT($str, $start, $end - $start + 1, "");
END LOOP;
END;
위험하기 때문에 짝이 맞지 않는 개방 브래킷을 제거하는 것을 확인했습니다. 하지만 짝이 없는 폐쇄 브래킷은 무해하기 때문에 무시합니다.
mysql> select strip_tags('<span>hel<b>lo <a href="world">wo<>rld</a> <<x>again<.');
+----------------------------------------------------------------------+
| strip_tags('<span>hel<b>lo <a href="world">wo<>rld</a> <<x>again<.') |
+----------------------------------------------------------------------+
| hello world again. |
+----------------------------------------------------------------------+
1 row in set
즐거운 시간 되세요.
컨텐츠가 항상 태그로 시작하는 경우(<body> 등)
시도해 보십시오.
SELECT * from table WHERE colmn_name REGEXP '>[^<]*mytext';
여기서는 부정적인 미리 보기 주장과 함께 정규식 일치가 필요합니다. "mytext" 뒤에 닫기 태그가 붙지 않습니다.MySQL은 8.0 현재에도 외관 주장을 지원하지 않지만 MariaDB는 지원합니다.질문에 대한 MariaDB 쿼리:
SELECT * FROM table WHERE column_name REGEXP 'mytext(?![^<>]*>)';
질문에 대한 또 다른 해결책은 일치하기 전에 일부/모든 태그를 제거하는 것입니다.REGEXP에 비해 비효율적이지만 효과도 있습니다.MySQL 8.0부터, MariaDB 10.0.5부터 기본 제공REGEXP_REPLACE
기능.strip_html'은 MariaDB 해당 문서 페이지의 첫 번째 예이기도 합니다.이러한 접근 방식에 대한 MySQL/MariaDB 쿼리:
SELECT * FROM table WHERE REGEXP_REPLACE (column_name, '<.+?>', '') LIKE '%mytext%';
그 외에도 질문의 문자열은 데이터와 표현을 혼합합니다.시스템 리소스의 낭비이므로 정기적으로 검색해서는 안 됩니다.
다음 줄 추가fnStripTags
기능.
끝나고SET Dirty = Insert( Dirty, iStart, iLength, '');
set Dirty = Replace(Dirty,' ',''); #No space between & and nbsp;
set Dirty = Replace(Dirty,'\r','');
set Dirty = Replace(Dirty,'\n','');
악센트 문자 등의 html 코드를 제거하기 위해 약간의 모드와 함께 위의 strip_tags()를 사용했습니다.이와 같습니다.
...BEGIN
DECLARE $start, $end INT DEFAULT 1;
SET $str = REPLACE($str, " ", " ");
SET $str = REPLACE($str, "€", "€");
SET $str = REPLACE($str, "á", "á");
SET $str = REPLACE($str, "é", "é");
SET $str = REPLACE($str, "í", "í");
SET $str = REPLACE($str, "ó", "ó");
SET $str = REPLACE($str, "ú", "ú");
LOOP...
MySQL 스트립 태그 구현을 통해 특정 태그를 대상으로 지정할 수 있으므로 각 함수 호출을 통해 태그를 하나씩 교체할 수 있습니다.태그 매개 변수만 전달하면 됩니다. 예를 들어,'a'
모든 개방/closing 앵커 태그 교체
# MySQL function to programmatically replace out specified html tags from text/html fields
# run this to drop/update the stored function
DROP FUNCTION IF EXISTS `strip_tags`;
DELIMITER |
# function to nuke all opening and closing tags of type specified in argument 2
CREATE FUNCTION `strip_tags`($str text, $tag text) RETURNS text
BEGIN
DECLARE $start, $end INT DEFAULT 1;
SET $str = COALESCE($str, '');
LOOP
SET $start = LOCATE(CONCAT('<', $tag), $str, $start);
IF (!$start) THEN RETURN $str; END IF;
SET $end = LOCATE('>', $str, $start);
IF (!$end) THEN SET $end = $start; END IF;
SET $str = INSERT($str, $start, $end - $start + 1, '');
SET $str = REPLACE($str, CONCAT('</', $tag, '>'), '');
END LOOP;
END;
| DELIMITER ;
# test select to nuke all opening <a> tags
SELECT
STRIP_TAGS(description, 'a') AS stripped
FROM
tmpcat;
# run update query to replace out all <a> tags
UPDATE tmpcat
SET
description = STRIP_TAGS(description, 'a');
Boann에서 제공하는 strip_tags 기능을 확장했습니다.이제 태그 사이의 구문을 유지하거나 무시하는 데 사용할 수 있습니다.코드 내 부분에 빈 태그가 있는 버그(즉, $keep_phrase = false)를 기록합니다.
CREATE FUNCTION strip_tags($str text, $tag text,$keep_phrase bool) RETURNS text
BEGIN
DECLARE $start, $end INT DEFAULT 1;
SET $str = COALESCE($str, '');
LOOP
SET $start = LOCATE(CONCAT('<', $tag), $str, $start);
IF (!$start) THEN RETURN $str; END IF;
IF ($keep_phrase) THEN
SET $end = LOCATE('>', $str, $start);
IF (!$end) THEN SET $end = $start; END IF;
SET $str = INSERT($str, $start, $end - $start + 1, '');
SET $str = REPLACE($str, CONCAT('</', $tag, '>'), '');
ELSE
SET $end = LOCATE(CONCAT('</', $tag, '>'),$str,$start);
IF (!$end) THEN
SET $end = LOCATE('/>',$str,$start);
SET $str = INSERT($str, $start, $end - $start + 2, '');
ELSE
SET $str = INSERT($str, $start, $end - $start
+ LENGTH(CONCAT('</', $tag, '>')), '');
END IF;
END IF;
END LOOP;
END //
기능을 증명하는 방법
SELECT strip_tags('<p>so<span id="x"> very</span> cool</p><p>so<span id="y"> very</span> cool</p>','span',true);
<p>so very cool</p><p>so very cool</p>
SELECT strip_tags('<p>so<span id="x"> very</span> cool</p><p>so<span id="y"> very</span> cool</p>','span',false);
<p>so cool</p><p>so cool</p>
$keep_phrase = false가 포함된 빈 요소는 지원되지 않습니다. 다음을 참조하십시오.
SELECT strip_tags('<p>so<span id="x"> very</span> cool</p><span/><p>so<span id="y"> very</span> cool</p>','span',false);
<p>so cool</p> cool</p>
저는 그렇게 비범한 논리는 필요 없다고 생각합니다.이렇게 단순화할 수 있습니다.
-- set @StrRow = 'this is first <a href="mytext.txt">row</a> from the table';
-- set @StrRow = 'THis is the second row <img src ="mytext.jpg"> my image is there';
set @StrRow = '<p>This is the Third row my mytext is there </p>';
-- set @StrRow = '<p class="te_mytext">This is the Third row my text is there </p>';
set @MyText = 'mytext';
select locate('<', @StrRow, locate(@MyText, @StrRow)) as '<', locate(@MyText, @StrRow) as MyText, locate('>', @StrRow, locate(@MyText, @StrRow)) as '>'
from xyz
where
locate('<', @StrRow, locate(@MyText, @StrRow)) > 0 and
locate('<', @StrRow, locate(@MyText, @StrRow)) < locate('>', @StrRow, locate(@MyText, @StrRow))
SQL 쿼리 내부의 HTML을 구문 분석할 수 없습니다. 말이 안 됩니다.모든 HTML이 삭제된 상태에서 테이블의 특별한 검색 버전을 유지할 수 있을지도 모르지만, 이를 위해서는 외부 처리를 사용해야 할 것입니다.
언급URL : https://stackoverflow.com/questions/2627940/remove-html-tags-from-record
'code' 카테고리의 다른 글
getActionBar()가 null을 반환합니다. (0) | 2023.11.04 |
---|---|
pom.xml에서 목표 지정 (0) | 2023.11.04 |
ASP.NET MVC5 - Oracle Database에 사용자 유지 (0) | 2023.11.04 |
Python Requests 라이브러리에서 기본 HTTP 인증을 사용하려면 어떻게 해야 합니까? (0) | 2023.10.30 |
angular2에서 타이머를 만드는 방법 (0) | 2023.10.30 |