php 초보인데 스트링을 잘라 출력하려는게 의도대로 잘 안되네요.

interoasis의 이미지

DB에 저장된 문자열을 가져와서 출력을 하려는데 의도한대로 출력이 잘 안되네요.

$data = mysql_fetch_array($result);

1. 우선 <?=$data['post_contents']?>라고하면 내용(한글이든 영어이든)이 정상적으로 출력이 됩니다.

2. DB에 저장된 내용이 모두 영어라면 <?=substr($data['post_contents'],0,10)?>해도 정상적으로 출력이 됩니다.

3. DB에 저장된 내용이 한글이기에 다음의 함수를 이용했습니다.

function ksubstr($str, $start, $end = 1)
{
$lenth = strlen($str);

$j = 0;
for($i = 0; $i < $lenth; $i++) {
$tmp = substr($str, $i, 1);
if(ord($tmp) > 127) {
$str_a[$j] = substr($str, $i, 2);
++$i;
}
else { $str_a[$j] = $tmp; }
++$j;
}

if($end > 0) $end += $start;
else $end = sizeof($str_a) + $end;

for($i = $start; $i < $end; $i++) {
$str_r .= $str_a[$i];
}
return $str_r;
}

4. 위의 함수를 이용하여 <?=ksubstr('가나다라마바사..', 0, 10)?>을 하면 정상적으로 출력이 됩니다.

5. 그런데 <?=ksubstr($data['post_contents'], 0, 10)?>을 하면 출력이 안됩니다.

혹시나해서 다른루틴의 함수를 참고해도 직접 변수를 입력하면 잘 출력이 되는데 DB의 스트링을 변수로 출력하려면 아무것도 출력이 안됩니다.mb_substr도 마찬가지구요. 1번처럼 그냥 출력하면 출력이 되는데말이죠. 캐릭터셋은 UTF-8을 쓰고있고 PHP버전은 5.3.2입니다. 무엇이 문제인걸까요?

pogusm의 이미지

$data['post_contents'] 의 문자 개수가 10개 이하일때,
ksubstr에서 그부분을 체크하지 않아서
오류가 발생해서 출력되지 않은게 아닐까요?

그리고
$str_a[$j] = substr($str, $i, 2);
++$i;
이부분도...
$str_a[$j] = substr($str, $i, 3);
$i=$i+2;
으로 변경해야 될거 같은데요...

interoasis의 이미지

DB 접속할시에 쿼리문에다가 mysql_query("set names utf8");을 첨가해주니 잘되네요.
캐릭터셋을 제대로 설정해주지 않아서 생긴 문제였나 봅니다. 답변해주셔서 감사합니다;;

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.