예를 들어...
br태그를 XHTML형식인 br/로 바꾸는 것을 만들때요.
$text = '<br class="abcd">
<br>
<br >
<br/>';
$pattern = '/<br(.*)>/';
$replacement = '<br\1/>';
$result = preg_replace($pattern, $replacement, $text);
print_r($result);
이렇게 하면
<br class="abcd"/>
<br/>
<br />
<br//>
대체로 잘 나오지만 마지막에 '이미 잘 되어 있는' br/태그일경우 br//로 슬래시가 두개가 되어버립니다.
이런 경우 어떻게 해야 할까요.
/를 인식시키려면 $pattern을 바꿔줘야 하겠는데, 정규식 생초보라 잘 안되네요 ^^
br태그를 XHTML형식인 br/로 바꾸는 것을 만들때요.
$text = '<br class="abcd">
<br>
<br >
<br/>';
$pattern = '/<br(.*)>/';
$replacement = '<br\1/>';
$result = preg_replace($pattern, $replacement, $text);
print_r($result);
이렇게 하면
<br class="abcd"/>
<br/>
<br />
<br//>
대체로 잘 나오지만 마지막에 '이미 잘 되어 있는' br/태그일경우 br//로 슬래시가 두개가 되어버립니다.
이런 경우 어떻게 해야 할까요.
/를 인식시키려면 $pattern을 바꿔줘야 하겠는데, 정규식 생초보라 잘 안되네요 ^^
[이 게시물은 관리자님에 의해 2011-10-31 17:16:08 PHP & HTML에서 이동 됨]
댓글 5개
17년 전
$text = '<br class="abcd">
<br>
<br >
<br/>';
$pattern = '/<br([^\/]*)>/i';
$replacement = '<br$1/>';
$result = preg_replace($pattern, $replacement, $text);
print_r($result);
이것도 되네요. ^^
http://phpschool.com/guild/guild_main.php?gd_num=85
<br>
<br >
<br/>';
$pattern = '/<br([^\/]*)>/i';
$replacement = '<br$1/>';
$result = preg_replace($pattern, $replacement, $text);
print_r($result);
이것도 되네요. ^^
http://phpschool.com/guild/guild_main.php?gd_num=85
17년 전
br뒤에 태그가 바로 올 수 있으니.. (.*) 보단 ([^>]*)가 맞습니다.
공백이나 /는 무시하고 강제로 치환하면되니.. ()에서 빼고..
대소문자 구분안하기 /i 옵션 추가..
---
$pattern = '/<br[[:space:]\/]*([^>]*)>/i';
$replacement = '<br \1/>';
공백이나 /는 무시하고 강제로 치환하면되니.. ()에서 빼고..
대소문자 구분안하기 /i 옵션 추가..
---
$pattern = '/<br[[:space:]\/]*([^>]*)>/i';
$replacement = '<br \1/>';
17년 전
<?php
$text = <<<TEXT
<br class="abcd"/> 같이 태그내에 설정값이 있고 뒤에 /가 있을 경우엔
<br class="abcd"//> 같이 슬래시가 두개로 변환되는데 이 경우는 어떻게 해야 할까요.
<br class="abcd">
<br>
<br />
<br/> <br//>
TEXT;
echo preg_replace_callback('@<br(.*?)/*>@', 'trim_callback', $text);
function trim_callback($matches)
{
return '<br '.trim($matches[1]).'/>';
}
?>
$text = <<<TEXT
<br class="abcd"/> 같이 태그내에 설정값이 있고 뒤에 /가 있을 경우엔
<br class="abcd"//> 같이 슬래시가 두개로 변환되는데 이 경우는 어떻게 해야 할까요.
<br class="abcd">
<br>
<br />
<br/> <br//>
TEXT;
echo preg_replace_callback('@<br(.*?)/*>@', 'trim_callback', $text);
function trim_callback($matches)
{
return '<br '.trim($matches[1]).'/>';
}
?>
17년 전
http://phpschool.com/gnuboard4/bbs/board.php?bo_table=qna_function&wr_id=233784&sca=&sfl=mb_id&stx=kagla&sop=and
17년 전
<?php
$text = <<<TEXT
<br class="abcd"/> 같이 태그내에 설정값이 있고 뒤에 /가 있을 경우엔
<br class="abcd"//> 같이 슬래시가 두개로 변환되는데 이 경우는 어떻게 해야 할까요.
<br class="abcd">
<br>
<br />
<br/> <br//>
TEXT;
$text = preg_replace("`<br([^>]*)`ie", "'<br' . stripslashes(str_replace('/', '', '\\1')) . '/'", $text);
?>
<textarea rows=10 cols=100><?=$text?></textarea>
$text = <<<TEXT
<br class="abcd"/> 같이 태그내에 설정값이 있고 뒤에 /가 있을 경우엔
<br class="abcd"//> 같이 슬래시가 두개로 변환되는데 이 경우는 어떻게 해야 할까요.
<br class="abcd">
<br>
<br />
<br/> <br//>
TEXT;
$text = preg_replace("`<br([^>]*)`ie", "'<br' . stripslashes(str_replace('/', '', '\\1')) . '/'", $text);
?>
<textarea rows=10 cols=100><?=$text?></textarea>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7930 | 9년 전 | 476 | ||
| 7929 | 9년 전 | 410 | ||
| 7928 | 9년 전 | 486 | ||
| 7927 | 9년 전 | 390 | ||
| 7926 | 9년 전 | 696 | ||
| 7925 | 9년 전 | 421 | ||
| 7924 | 9년 전 | 403 | ||
| 7923 | 9년 전 | 390 | ||
| 7922 | 9년 전 | 420 | ||
| 7921 | 9년 전 | 435 | ||
| 7920 | 9년 전 | 345 | ||
| 7919 | 9년 전 | 356 | ||
| 7918 | 9년 전 | 510 | ||
| 7917 | 9년 전 | 363 | ||
| 7916 | 9년 전 | 452 | ||
| 7915 | 9년 전 | 467 | ||
| 7914 | 9년 전 | 476 | ||
| 7913 | 9년 전 | 655 | ||
| 7912 | 9년 전 | 489 | ||
| 7911 | 9년 전 | 409 | ||
| 7910 | 9년 전 | 460 | ||
| 7909 | 9년 전 | 574 | ||
| 7908 | 9년 전 | 512 | ||
| 7907 | 9년 전 | 447 | ||
| 7906 | 9년 전 | 468 | ||
| 7905 | 9년 전 | 434 | ||
| 7904 | 9년 전 | 421 | ||
| 7903 | 9년 전 | 434 | ||
| 7902 | 9년 전 | 606 | ||
| 7901 |
|
9년 전 | 772 | |
| 7900 | 9년 전 | 666 | ||
| 7899 | 9년 전 | 450 | ||
| 7898 | 9년 전 | 448 | ||
| 7897 | 9년 전 | 408 | ||
| 7896 | 9년 전 | 423 | ||
| 7895 | 9년 전 | 542 | ||
| 7894 | 9년 전 | 447 | ||
| 7893 | 9년 전 | 425 | ||
| 7892 | 9년 전 | 461 | ||
| 7891 | 9년 전 | 818 | ||
| 7890 | 9년 전 | 1237 | ||
| 7889 | 9년 전 | 772 | ||
| 7888 |
limsy1987
|
9년 전 | 585 | |
| 7887 | 9년 전 | 636 | ||
| 7886 | 9년 전 | 523 | ||
| 7885 | 9년 전 | 494 | ||
| 7884 | 9년 전 | 486 | ||
| 7883 | 9년 전 | 493 | ||
| 7882 | 9년 전 | 550 | ||
| 7881 | 9년 전 | 527 | ||
| 7880 | 9년 전 | 641 | ||
| 7879 | 9년 전 | 531 | ||
| 7878 | 9년 전 | 1302 | ||
| 7877 | 9년 전 | 825 | ||
| 7876 | 9년 전 | 567 | ||
| 7875 | 9년 전 | 640 | ||
| 7874 |
|
9년 전 | 835 | |
| 7873 | 9년 전 | 563 | ||
| 7872 | 9년 전 | 731 | ||
| 7871 | 9년 전 | 546 | ||
| 7870 | 10년 전 | 666 | ||
| 7869 | 10년 전 | 481 | ||
| 7868 | 10년 전 | 522 | ||
| 7867 | 10년 전 | 529 | ||
| 7866 | 10년 전 | 586 | ||
| 7865 | 10년 전 | 530 | ||
| 7864 | 10년 전 | 583 | ||
| 7863 | 10년 전 | 578 | ||
| 7862 | 10년 전 | 546 | ||
| 7861 | 10년 전 | 714 | ||
| 7860 | 10년 전 | 695 | ||
| 7859 | 10년 전 | 465 | ||
| 7858 | 10년 전 | 776 | ||
| 7857 | 10년 전 | 1161 | ||
| 7856 | 10년 전 | 585 | ||
| 7855 | 10년 전 | 824 | ||
| 7854 | 10년 전 | 757 | ||
| 7853 | 10년 전 | 662 | ||
| 7852 | 10년 전 | 583 | ||
| 7851 | 10년 전 | 586 | ||
| 7850 | 10년 전 | 655 | ||
| 7849 | 10년 전 | 426 | ||
| 7848 | 10년 전 | 492 | ||
| 7847 | 10년 전 | 731 | ||
| 7846 | 10년 전 | 516 | ||
| 7845 | 10년 전 | 493 | ||
| 7844 | 10년 전 | 455 | ||
| 7843 | 10년 전 | 492 | ||
| 7842 | 10년 전 | 476 | ||
| 7841 | 10년 전 | 445 | ||
| 7840 | 10년 전 | 463 | ||
| 7839 | 10년 전 | 515 | ||
| 7838 | 10년 전 | 576 | ||
| 7837 | 10년 전 | 404 | ||
| 7836 | 10년 전 | 452 | ||
| 7835 | 10년 전 | 535 | ||
| 7834 |
|
10년 전 | 1232 | |
| 7833 | 10년 전 | 499 | ||
| 7832 | 10년 전 | 475 | ||
| 7831 | 10년 전 | 639 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기