메뉴 건너뛰기

프로그램언어

2015.04.14 19:07

문자열 치환하기

조회 수 20508 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

str_replace(), substr_replace() 함수


string str_replace(string needle, string str, string haystack) ;

str_replace() 함수는 haystack 의 데이터 안에서 needle 의 데이터(교체할 문자열)를 찾아 str 의 데이터(대체문자열)로 모두 변경해준다.

$string = "안녕하세요. 좋은 아침 입니다.\n" ;

echo str_replace("아침", "저녁", $string) ;

// "아침"을 "저녁"으로 치환 한다.

결과 : 안녕하세요. 좋은 저녁 입니다.


$str = "<textarea cols='50' rows='7' name='text' >".str_replace('<br>','\r\n',$row[text])."</textarea>";


<textarea name="content"></textarea>
<?php

    $clean_content = htmlspecialchars($_POST[content], ENT_QUOTES);
    $clean_content = str_replace("\r\n","<br/>",$clean_content); //줄바꿈 처리
    $clean_content = str_replace("\u0020","&nbsp;",$clean_content); // 스페이스바 처리
?>


<?php
include "connect.php"; //mysql DB접속
$tblName = sample; // 테이블명
$query="SELECT * FROM $tblName;"; // 테이블에 있는 모든 자료 쿼리, 보통은 필요한 항목만 지정하는게 좋음
$result=mysql_query($query, $connect); //쿼리문입력
$row = mysql_fetch_array ($result); //연관배열만들기
$content = nl2br($row['content']);  // Textarea 영역 개행문자 처리

// 폼에서 받아온 데이터에 개행처리된 문자열을 자동으로 <br />태그를 붙여주는 역할을 하는 함수
echo $row['content'];
?>


string substr_replace(string string, string replacement, int start, int [length]) ;

substr_replace() 함수는 string 의 데이터를 start 번째 문자부터 length 길이 만큼을 replacement 의 데이터로 변경한다.

$string = "안녕하세요. 좋은 아침 입니다.
echo substr_replace($string, "저녁", strpos($string, "아침"), 6);

결과 : 안녕하세요. 좋은 저녁 입니다.


  1. stripcslashes — addcslashes()로 인용한 문자열을 되돌림

    Date2016.12.23 Views20465
    Read More
  2. quotemeta 모든 메타 문자앞에 역슬래쉬를 붙인 문자열을 반환

    Date2016.12.23 Views20466
    Read More
  3. stripslashes — 따옴표 처리한 문자열을 풉니다

    Date2016.12.23 Views20490
    Read More
  4. 문자열 치환하기

    Date2015.04.14 Views20508
    Read More
  5. [PHP] 한글명 파일 다운로드받기

    Date2014.03.26 Views20561
    Read More
  6. HTTP Protocol의 data method - GET / POST

    Date2016.04.22 Views20642
    Read More
  7. PHP 소스 : 이미지 리사이즈, 섬네일

    Date2014.03.26 Views20709
    Read More
  8. 템플릿 관련 정보

    Date2016.08.22 Views20739
    Read More
  9. 검색어 처리 루틴

    Date2015.04.14 Views20780
    Read More
  10. array_slice 배열의 일부를 추출

    Date2016.12.23 Views20781
    Read More
  11. preg_match (정규표현식 매치를 수행합니다)

    Date2016.12.23 Views20857
    Read More
  12. PHP error 메시지 출력

    Date2015.04.14 Views20930
    Read More
  13. PHP switch 문

    Date2015.04.14 Views20996
    Read More
  14. Ajax로 구연한 실시간 서버시간출력

    Date2017.03.06 Views21041
    Read More
  15. RSSReader Class 제작 및 Reader 만들기

    Date2016.08.22 Views21046
    Read More
  16. FPDF - PHP로 PDF 만들기

    Date2014.02.27 Views21089
    Read More
  17. PHP continue 문

    Date2015.04.14 Views21098
    Read More
  18. 체크박스, post 로 넘기고 받아서 다시 체크하기, checkbox

    Date2017.03.07 Views21141
    Read More
  19. POST값 통째로 인코딩하기

    Date2015.04.06 Views21179
    Read More
  20. DB 내용을 화면에 출력(이중 for 문)

    Date2015.04.14 Views21254
    Read More
Board Pagination Prev 1 ... 8 9 10 11 12 13 14 15 16 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved