"PHP str word count()"의 두 판 사이의 차이

67번째 줄: 67번째 줄:
==같이 보기==
==같이 보기==
*[[PHP 문자열]]
*[[PHP 문자열]]
==참고 자료==
*http://php.net/manual/en/function.str-word-count.php
*http://www.w3schools.com/php/func_string_str_word_count.asp


[[분류: PHP 문자열]]
[[분류: PHP 문자열]]

2015년 11월 14일 (토) 11:24 판

1 개요

PHP str_word_count()
  • 문자열의 단어를 세거나, 단어 배열로 바꾸는 PHP 함수

2 단어 개수

echo str_word_count("Hello world!");
# 2
echo str_word_count("Hello my fri3nd!");
# 4

3 단어 배열

  • 단어 배열로 만들기
print_r( str_word_count("Hello my fri3nd!", 1) );
# Array
# (
#     [0] => Hello
#     [1] => my
#     [2] => fri
#     [3] => nd
# )
print_r( str_word_count("Hello, my fri3nd. Stay awhile & listen.", 1) );
# Array
# (
#     [0] => Hello
#     [1] => my
#     [2] => fri
#     [3] => nd
#     [4] => Stay
#     [5] => awhile
#     [6] => listen
# )
→ 특수문자, 숫자는 단어에 포함되지 않고 공백처럼 인식됨
print_r( str_word_count("Hello, my fri3nd. Stay awhile & listen.", 1, "3&")) ;
# Array
# (
#     [0] => Hello
#     [1] => my
#     [2] => fri3nd
#     [3] => Stay
#     [4] => awhile
#     [5] => &
#     [6] => listen
# )
→ 3와 &을 일반 영문자처럼 인식되도록 함
  • 단어 배열로 만들되 key를 시작위치로 지정
print_r( str_word_count("Hello world!", 2) );
# Array
# (
#     [0] => Hello
#     [6] => world
# )

4 같이 보기

5 참고 자료

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}