PHP: String functions - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<stream_set_write_bufferAddCSlashes>
view the version of this page
Last updated: Wed, 29 Jan 2003

XCIX. String functions

이들 함수는 모두 다양한 방법으로 문자열을 다룬다. 정규 표현 섹션과 URL 핸들링 섹션에서좀 더 다양하고 특별한 함수들을 만나 볼 수 있다.

어떻게 문자열이 적용되고, 특히 작은 따옴표와 큰따옴표 그리고 에스케이프 시퀀스(escape sequence)와 어떤 관계가 있는지는 이 메뉴얼의 Types섹션의 Strings편을 참조하기 바란다.

차례
AddCSlashes -- C형태로 문자를 인용한다.
AddSlashes -- 슬래쉬로 문자를 인용한다.
bin2hex --  십진 데이터를 16진수 표현으로 변환한다.
chop -- 뒤부분의 공백을 제거한다.
chr -- 특정 문자를 반환한다.
chunk_split -- Split a string into smaller chunks
convert_cyr_string --  키릴 문자(Cyrillic character) 셋을 다른것으로 변환한다.
count_chars --  문자열에 사용된 글자에 관한 정보를 반환한다.
crc32 -- crc32 다항식의 문자열을 계산한다
crypt -- 문자열을 DES-암호화 한다.
echo -- 하나 혹은 그 이상의 문자열을 출력한다.
explode -- 문자열을 주어진 문자열을 기주으로 분리한다.
fprintf -- Write a formatted string to a stream
get_html_translation_table --  htmlspecialchars()htmlentities()에 사용된 translation 테이블을 반환한다.
hebrev --  논리적인 헤브루 텍스트를 알아볼 수 있는 텍스트로 변환한다.
hebrevc --  개행 문자를 포함한 헤프류 텍스트를 볼 수 있는 텍스트로 변환한다.
html_entity_decode --  Convert all HTML entities to their applicable characters
htmlentities --  해당되는 모든 문자를 HTML 엔터티 형태로 변환한다.
htmlspecialchars --  특수 문자를 HTML 엔터티 형태로 변환한다.
implode -- 문자열로 함수 엘리먼트를 연결한다.
join -- 문자열로 배열의 원소를 연결한다.
levenshtein --  두 문자열 간의 Levenshtein 디스턴스를 계산한다.
localeconv -- Get numeric formatting information
ltrim --  문자열 시작부분에서 공백을 제거한다.
md5_file -- Calculates the md5 hash of a given filename
md5 -- 문자열의 md5 해쉬를 계산한다.
metaphone -- 문자열의 메타폰 키(metaphone key) 를 계산한다.
money_format -- Formats a number as a currency string
nl_langinfo --  Query language and locale information
nl2br -- 뉴라인을 HTML 라인 브레이크(<BR>)로 변환한다.
number_format -- Format a number with grouped thousands
ord -- 문자의 ASCII 값을 반환한다.
parse_str -- 문자열을 변수로 파싱한다.
print -- 문자열을 출력한다.
printf -- 포멧된 문자열을 출력한다.
quoted_printable_decode --  인용된 출력가능한 문자열을 8비트 문자열로 변환한다.
quotemeta -- 메타 문자를 인용한다.
rtrim -- 문자열 끝의 공백을 제거한다.
setlocale -- 지역적보를 지정한다.
sha1_file -- Calculate the sha1 hash of a file
sha1 -- Calculate the sha1 hash of a string
similar_text --  두 문자열 간의 유사성을 계산한다.
soundex -- 문자열의 soundex 키를 계산한다.
sprintf -- 포멧된 문자열을 반환한다.
sscanf -- 포멧이 따라서 입력을 문자열로부터 파싱한다.
str_pad -- 다른 문자로 특정 길이만큼 문자열을 채운다.
str_repeat -- 문자열을 반복한다.
str_replace --  haystack 의 모든 needle을 str로 변환한다.
str_rot13 -- Perform the rot13 transform on a string
str_shuffle -- Randomly shuffles a string
str_word_count --  Return information about words used in a string
strcasecmp --  대소문자를 구분하지 않는 2진수 형태의 문자열을 비교 (Binary safe case-insensitive string comparison)
strchr --  문자가 처음 나타난 위치를 찾는다.
strcmp -- 2진 형태의 문자열을 비교
strcoll -- Locale based string comparison
strcspn --  마스크와 매칭되지 않는 초기 세그먼트의 길이 찾는다.
strip_tags -- 문자열로 부터 HTML 태그와 PHP 태그를 없앤다
stripcslashes --  addcslashes()로 quote 된 문자열을 un-quote 한다.
stripslashes --  addslashes()로 quote 된 문자열을 un-quote 한다.
stristr --  대소문자를 구별하지 않는 strstr()
strlen -- 문자열의 길이를 구한다.
strnatcasecmp --  대소문자 구별 없이 "natural order" 알고리즘으로 문자열을 비교한다.
strnatcmp --  "natural order" 알고리즘을 이용한 문자열 비교
strncasecmp --  대소문자를 분하지 않는 2진수 형태의 문자열의 처음 n 개 문자를 비교 - Binary safe case-insensitive string comparison of the first n characters
strncmp --  처음 n 문자의 binary safe 문자열 비교
strpos --  문자열에서 처음 위치를 반환한다.
strrchr --  문자열에서 가장 마지막의 어커런스를 찾는다.
strrev -- 문자열을 뒤집는다.
strrpos --  문자열 안에서 문자의 마지막 출현 위치를 반환한다.
strspn --  마스크에 매칭되는 초기 세그먼트의 길이를 찾는다.
strstr -- 문자열의 처음 어커런스를 찾는다
strtok -- 문자열을 토큰화한다.
strtolower -- 문자열을 소문자로 만든다.
strtoupper -- 문자열을 대문자로 만든다.
strtr -- 특정 문자를 번역한다.
substr_count -- 부분문자열의 수를 센다
substr_replace -- 문자열의 일부를 치환한다.
substr -- 문자열의 일부를 반환한다.
trim --  문자열의 처음과 끝에 있는 공백을 제거한다.
ucfirst -- 문자열의 처음 글자를 대문자로 만든다.
ucwords --  문자열에 있는 각 단어의 처음 글자를 대문자로 바꾼다.
vprintf -- Output a formatted string
vsprintf -- Return a formatted string
wordwrap --  정지문자를 이용해 주어진 수 만큼의 문자를 래핑한다.


User Contributed Notes
String functions
add a note add a note
heiko at individual-web dot com
07-Mar-2000 03:24

I had the problem that in some of the guestbooks I programmed, people entererd trash like 400 characters without spaces and such.  Without any spaces there was no way to break the lines and the whole guestbooked looked screwed.  I wrote this little function that makes sure no words are more than a specified number of characters long.  If a word is too long, spaces are inserted to get the word to the maximum length allowed. Maybe it's useful to some of you:

function strmaxwordlen($input,$len) {
$l = 0;
$output = "";
for ($i = 0; $i < strlen($input); $i++) {
 $char = substr($input,$i,1);
 if ($char != " ") { $l++; } else { $l = 0; }
 if ($l == $len) { $l = 0; $output .= " "; }
 $output .= $char;
}
return($output);
}

[Editor's note:

Alternativaly, wordwrap() function is available for this purpose

Maxim]

mfawcett at tir dot com
24-Apr-2000 07:45

For a MySQL problem (where a user might put quotes in a text field from a form) use addslashes() to escape all quotes (single and double).  For example:

// Assuming 'textstuff' is from a form
$mytextstuff = addslashes($textstuff);
$sql = "insert into MyTable set MyField='$mytextstuff'";

Also if you are working with html forms and/or text fields from databases a lot you'll probably want to read up on commands like htmlspecialchars() as well (a field like "foo<bar>" might not throw MySQL off but it won't display correctly in an html page).

ognio at peruserver dot com
21-Sep-2000 12:39

In order to properly display text from a form into an html page you need code
like this:

<? echo htmlentities(stripslashes($_GET['form_text'])); ?>

R dot J dot Vrijhof at bigfoot dot com
26-Dec-2000 03:20

I wanted to convert numbers to an adjective form (so from "1" to "1st", from "352" to "352nd", etc.), but couldn't find it in PHP, so I wrote this little code to do that for me. Hope you find it useful:

function nth($in) {
 if (!is_long($in))
   return(-1);
 switch ($in) {
   case 0 : return("0th");
   case 1 : return("1st");
  case 2 : return("2nd");
   case 3 : return("3rd");
   case 4 :
   case 5 :
   case 6 :
   case 7 :
   case 8 :
   case 9 :
   case 10 :
   case 11 :
   case 12 :
   case 13 :
   case 14 :
   case 15 :
   case 16 :
   case 17 :
   case 18 :
   case 19 : return($in."th");
 }
 switch (substr($in, -1, 1)) {
   case 0 : return($in."th");
  case 1 : return($in."st");
   case 2 : return($in."nd");
   case 3 : return($in."rd");
   case 4 :
   case 5 :
   case 6 :
   case 7 :
   case 8 :
   case 9 : return($in."th");
   default : return(0);
}
}

It will return -1 when the input is not an integer and 0 when it couldn't find a match (although that shouldn't happen).

david at audiogalaxy dot com
13-Jun-2001 08:31

Browsers (Internet Explorer at least) won't wrap text immediately before puntuation . ? ! ( ) [ ] { } / even if the preceeding character is whitespace.  I believe this is intentional widow/orphan prevention behavior.  This complicates the processing of user-inputed text (e.g. the message board junk problem).  Even if you use <textarea wrap=hard> to force newlines into the input text (which is imperfect since a clever user can just modify the HTML before posting), people posting long strings of punctuation characters, or just punctuation characters at the beginning of each line in the <textarea> will mess up the format of the page for users viewing their post.

You obviously can't solve this by just inserting a spaces - the browser still won't wrap.  My solution is to use str_replace to put a &nbsp; (the least visually impacting non-whitespace character I can think of) between the \n and any pucntuation so the wrap can happen in the whitespace before the &nbsp;.  Kindof ironic to be using &nbsp; to *cause* a break:)

[Editor's note:

You could also use wordwrap() function

Maxim Maletsky]

thanatos at wol dot be
15-Jun-2001 10:45

I have written an *evil* little function that will cut a string on a character given. Features:
1) Minimum length.
2) If a string doesn't contain the character, it cuts your string to minimum length.
3) If the next character beyond the minimumlength equals the given character, it will cut the string to minimulength as well.
Code:

<?php
function cuttext($tring,$cuton) {

/* Define the character to cut on */
/* One could make an array with all characters to cut on */
/* But I wouldn't know how to :) */

$space=" ";

if (!strstr($tring,$space)) {

/* No space is found in the whole string: cut at 20th character */

$tring=substr($tring,0,$cuton);
}
if (substr($tring,$cuton,1)==$space) {

/* 21st character is a space: cut at 20th character */

$tring=substr($tring,0,$cuton);
} else {

/* 21st Character is NOT a space: return to last space and cut there */

while ($teller <= $cuton) {
if (substr($tring,$cuton-$teller,1)==$space) {
$tring=substr($tring,0,$cuton-$teller);
break;
}
$teller++;
}
}
return $tring;
}
echo(cuttext("waf groink muha zz gruu wrrreea iii gargghhh >:)>:)",20));
?>

rs234 at cornell dot edu
04-Aug-2001 12:31

there's a problem when i used the addslashes function for my odbc connection... in order to fix it, I used the htmlspecialchars($str_var, ENT_QUOTES); instead and it worked fine.  example is as follow:

// $str - a string w/ single quotes
$str = htmlspecialchars($str, ENT_QUOTES);
.
.
.
.
$sql = "<something> $str <something>";
<etc.>

carl at _nospam_youngbloods dot org
10-Jan-2002 03:27

I've ported a function to PHP that will find the longest common substring in two different strings.  Here it is:

function LongestCommonSubstring($string1, $string2)
{
// find length of longest common substring first
$L = array();
$length1 = strlen($string1);
$length2 = strlen($string2);

for ($i = $length1; $i >= 0; $i--)
{
for ($j = $length2; $j >= 0; $j--)
{
if ($string1[$i] == '' || $string2[$j] == '') $L[$i][$j] = 0;
elseif ($string1[$i] == $string2[$j]) $L[$i][$j] =
      1 + $L[$i + 1][$j + 1];
else $L[$i][$j] = max($L[$i + 1][$j], $L[$i][$j + 1]);
}
}

// then find the actual substring
$substring = '';
$i = 0;
$j = 0;
 
while ($i < $length1 && $j < $length2)
{
if ($string1[$i] == $string2[$j])
{
$substring .= $string1[$i];
$i++; $j++;
}
elseif ($L[$i + 1][$j] >= $L[$i][$j + 1]) $i++;
else $j++;
}

return $substring;
}

Here is where I found the original:


captainbajoo at juno dot com
22-Apr-2002 12:09

This is a more compact function to get a numeric adjective.  No idea if it's faster than the other "nth" function here (though probably is, since "th" is more likely to occur), but the code is definitely shorter.

Assumes integer input.  Returns a string.

function getNth ($n) {
 $qn = (int) (($n%100) / 10);
 $rn = $n % 10;

 $suffix = "th";

 if ($qn != 1) {
   switch ($rn) {
   case 1: $suffix = "st"; break;
   case 2: $suffix = "nd"; break;
  case 3: $suffix = "rd"; break;
   }
 }

return $n . $suffix;
}

jewfish at jewfish dot net
05-Jun-2002 06:20

One could make an ever quicker isalphanumeric() function by using regular expressions:

function isalphanumeric($test) {
   return !(preg_match("/[^a-z,A-Z,0-9 ]/", $test));
}

Note: that is a space after the 9, in order to inclue spaces in the comparison. One could also add \t and \n so that it did not fail out if it found a tab or newline.  This is much cleaner and more efficient than the array approach described earlier, and nicer than the ordinal characters approach as well.

chris at earleweb_nospam dot com
13-Jul-2002 09:03

An alternative to the the "array_shift" way to change $color array:

$Counter = 0; //set to 1 if you want to start on #CCCCCC
$Colors = Array('#FFFFFF', '#CCCCCC');
$tableColor = $Colors[($Counter %= 2)];
/* $Counter %= 2; ensures $Counter is never greater than 1, in the off chance that you have a 32 thousand element array and really is not needed -- you could use $Counter % 2 if you really want to*/
$Counter++;

When ever $Counter is odd the modulus division will leave 1, but even numbers (and 0) mod 2 = 0.  Then Counter is added to so that the next phase of the loop will become the next color.

dr dot helga at prickel-pit dot de
14-Jul-2002 02:41

For generating a Field-Number File I needed a function, wich can add chars in front or at the end of a string to match a certain string length. I couldn't find it in php so here's the function I wrote to solve the problem:

--------------CUT------------------
function addChars($string,$char,$endlength,$dir) {
$i=0;
$stringlength=strlen($string);
$addchar_lenght=$endlength-$stringlength;
$addchar_string="";
while ($i<$addchar_lenght) {
$addchar_string.=$char;
$i++;
}
if ($dir==1) {
$new_string=$string.$addchar_string;
}
if ($dir==0) {
$new_string=$addchar_string.$string;
}
return $new_string;
}
----------------/CUT-----------------

$string is the original string
$char is the character which shall be added
$endlength is the length of the new string
$dir specifies if the chars shall be added in front (0) or at the end (1) of the string

example:
"1234" should be "00001234":

$text1="1234";
$text2=addChars($text,"0",8,0)

hope someone will find it usefull ...

bog2k<AT>softhome<DOT>net
18-Jul-2002 04:02

How about reversing the HTML Chars (&eacute; => �). Here is a function that is able to reverse any of HTML entitities regardles if the entity has the ";" termination. That means it can detect and replace "t&eacute;l&eacute;" => "t�l�", and also "t&eacutel&eacute" => "t�l�".

function ReverseHtmlChars ($str, $trans_table = HTML_ENTITIES) {

   $trans = get_html_translation_table ($trans_table);
   $trans2 = preg_replace ("';$'", "", $trans);
   $trans = array_flip ($trans);
   $trans2 = array_flip ($trans2);

   return strtr (strtr ($str, $trans), $trans2);
}

Also you can provide a second argument if you only want to replace the HTML specialchars.

I've read a note of the Editor in which he said that this is kinda pointless because normally you don't need to reverse HTML chars ... well, thats partially true.

In my case I really needes something like this because this way I can strip a html file of tags, reverse all HTML entities and obtain a very *human readable* text file !!! ... so its not so pointless.

P.S. you can also think of a similar function that has as second argument the translation table rather than 0/1 so you can have your own set of translations (very very useful for text translations)

Cheers,
BoG

global2b at yahoo dot com
21-Aug-2002 04:25

In case any of you wanted to parse a deliminated text file, I've come up with a script that may of some use.

//I call the function and return the parsed data in a multi-D array.

function thisone() {
$info = array();
$x=file("yourData.txt");

//Here I take the first line of the data file and grab the variable names.
$names = explode('|',$x[0]);

//Once I've got the variable names I delete x[0] so that I can run through the info array with foreach
unset($x[0]);

foreach($x as $keyx=>$valuex) {
$w = explode('|',$valuex);

foreach($w as $keyw=>$valuew) {
$info[$keyx][$names[$keyw]] = $valuew;
}
}

return $info;
}

$bin = thisone();
print_r($bin);

skip at seawana dot com
29-Aug-2002 10:24

// Here is a simpler way to swap two values (for alternating TABLE row colors for instance).
// Define your colors in an array:
$ar_colors = array ("#0066AA","#003399");

// Now simply display them and then reverse the array like this:
echo $ar_colors[0]; $ar_colors = array_reverse($ar_colors);

d dot gebuehr at ntz dot de
13-Sep-2002 10:25

I've written a small function, which extracts all positions of a given character from a string. It may be useful for example to modify html-tags with not known values. (It's used to get rid of class-arguments and mso-styles while importing MS-Word-Documents):

function charpos($searchstring,$delimiter) {
global $positions;
global $search_found;
if (strpos($searchstring,$delimiter) === FALSE) {
$search_found=0;
}
else {
  $search_found=1;
  $searchlen=strlen($searchstring);
  for ($i_s=0;$i_s<$searchlen;$i_s++){if (substr($searchstring,$i_s,1)==$delimiter){$positions[]=$i_s; } }
}
}

You can work with the positions-array, but remember to unset it before calling the function again, otherwise you'll fill it more and more :-)
Counting of positions begins with 0. If the charakter you search for ist not to be found in your searchstring, the function returns $search_found=0, otherwise $search_found will be 1.
If typing the function, be sure to have three "=" in asking whether the charakter was found.

discord at mac dot com
23-Oct-2002 11:30

If you are outputting to html, it might be better to insert a wordbreak (<wbr>) tag to allow a linebreak. If the text is too long and needs to be wrapped, the browser will break the line at that tag, otherwise, it ignores it. This is good because it won't even show up as a space if the browser doesn't wrap it at that point.

Here's an optimised preg_replace version of the strmaxwordlen function that uses this tag:

function splitLongWords($text, $max) {
����return preg_replace( '/([^\s]{'.$max.'})(?=[^\s])/', '$1<wbr>', $text);
}

(note: I didn't test this thoroughly, it may need some work :)

webmaster at gamequbed dot com
01-Feb-2003 10:14

I was recently working on a template engine, and i found that i wanted to add parameters to my template includes, for example,

{SUBJECTFIELD template="LINK_ROW" class="subjectfield"}

I found that this code worked out quite well! It converts the string into

$fileParam["SUBJECTFIELD"]["template"]

which will print

"LINK_ROW". It also works for multiple blocks. Have fun!

$text = "
{SUBJECTFIELD class=\"alternate\" rowtemplate=\"LINK_ROW\"}
{BODYFIELD class=\"alternate2\" rowtemplate=\"LINK_ROW\"}";

## remove quotes
$text = str_replace("\"","",$text);

## set counter
$counter = 0;

for ($i=0;$i<strlen($text);$i++)
{
## convert each {...} block into an array element
if ($text[$i] == "{")
{
$currentEndBrace = strpos($text,"}");

$param[$counter] = substr($text,$i,$currentEndBrace);

## remove the end brace
$text[$currentEndBrace] = "";

$counter++;
}
}

$fileParam = array();

for ($j=0;$j<count($param);$j++)
{
## remove trailing stuff
$param[$j] = rtrim($param[$j]);

## and the braces
$param[$j] = ereg_replace("{","",$param[$j]);
$param[$j] = ereg_replace("}","",$param[$j]);

## get the name of the template
$tempIdent = substr($param[$j],0,strpos($param[$j]," "));

## get the rest of the vars
$param[$j] = substr($param[$j],strpos($param[$j]," ")+1);

## make a new array
$fileParam[$tempIdent] = $param[$j];
}

while(list($key,$val) = each($fileParam))
{
## set up as a query string
$val = str_replace(" ","&",$val);

## parse it to a new array
parse_str($val,$eachVal);

## and assign it to another array
$fileParam[$key] = $eachVal;
}

kop at meme dot com
06-Mar-2003 11:49

function standardize_eol($s) {
 /* Return text with Unix end of line conventions.
  *
  * Input: $s  The string to convert.
  *
 * Returns: The string with Unix end of line conventions.
  *
  * Side Effects: none
  */
return strtr(ereg_replace("\r\n", "\n", $s), "\r", "\n");
}

To do the same thing to a file:

// Copy file, converting
// Windoze and mac eol sequences to Unix eol along the way.
     exec("sed -e 's/\r\$//' $old_path"
         . ' | tr "\\r" "\\n"'
          . " > $new_path");

Do _not_ try to set $old_path and $new_path to the same file.  Failure will be erratic.

missnglnk at missnglnk dot com
24-Mar-2003 04:35

Here's a better function to get the numerical thingy (1st, 2nd, 3rd, etc):

function nth ($n) {
  $rn = $n{strlen($n)};
   
  switch ($rn) {  
    case 1: $suffix = "st"; break;  
   case 2: $suffix = "nd"; break;  
    case 3: $suffix = "rd"; break;  
    default: $suffix = "th"; break;
  }
   
  return $n . $suffix;
}

szeryf at negativeIQ dot pl
01-Apr-2003 09:41

This is a little function I wrote for shortening strings to some arbitrary length. It may be useful if you want to display only headlines (or just a few starting sequences) of a larger text. The text is shortened to the last white space character before $len chars and an ellipsis (...) is added at the end. If no white space is found, the first $len chars (and ...) are returned. So the maximal length of returned value is always $len + 3. Here it goes:

function shorten ($var, $len = 120)
{
      if (empty ($var)) {
               return "";
      }
       if (strlen ($var) < $len) {
               return $var;
       }
       
       if (preg_match ("/(.{1,$len})\s/", $var, $match)) {
               return $match [1] . "...";
       }
       else {
              return substr ($var, 0, $len) . "...";
      }
}

It should be pretty self-explanatory. The regular expression matches up to $len chars followed by a white space char (\s). The match is "greedy", so it always tries to match "as many as possible".

add a note add a note

<stream_set_write_bufferAddCSlashes>
 Last updated: Wed, 29 Jan 2003
show source | credits | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: /
Last updated: Fri May 23 21:10:19 2003 CEST