本文目錄一覽:
- 1、php 怎麼處理字符串
- 2、(100分)[php]寫幾個你熟悉的字符串處理函數!
- 3、php去除字符串中空字符的常用方法
- 4、求字符串處理函數(全)
- 5、PHP字符串如何替換函數???
- 6、php哪個函數能取得字符串長度
php 怎麼處理字符串
大家通過對PHP的學習,可以運用這一高級語言創建一個性能較高的網站。對於初學者來說,對於PHP字符串mbstring還是比較陌生的,下面我們就來介紹一下PHP字符串mbstring的具體應用。
多國語言並存就意味着多字節,PHP內置的字符串長度函數strlen無法正確處理中文字符串,它得到的只是字符串所佔的字節數。對於GB2312的中文編碼,strlen得到的值是漢字個數的2倍,而對於UTF-8編碼的中文,就是1~3倍的差異了。
採用PHP字符串mbstring可以較好地解決這個問題。mb_strlen的用法和strlen類似,只不過它有第二個可選參數用於指定字符編碼。例如得到UTF-8的字符串$str長度,可以用mb_strlen($str,’UTF-8′)。如果省略第二個參數,則會使用PHP的內部編碼。內部編碼可以通過mb_internal_encoding()函數得到,設置有兩種方式:
1. 在php.ini中設置mbstring.internal_encoding = UTF-8
2. 調用mb_internal_encoding(”GBK”)
除了PHP字符串mbstring,還有很多切割函數,其中mb_substr是按字來切分字符,而mb_strcut是按字節來切分字符,但是都不會產生半個字符的現象。而且從函數切割對長度的作用也不同,mb_strcut的切割條件是小於strlen, mb_substr是等於strlen,看下面的例子,
? $str = ‘我是一串比較長的中文-’; echo “mb_substr:” . mb_substr($str, 0, 6, ‘utf-8′); echo ” “; echo “mb_strcut:” . mb_strcut($str, 0, 6, ‘utf-8′); ?
輸出如下:
mb_substr:我是一串比較
mb_strcut:我是
需要注意的是,PHP字符串mbstring並不是PHP核心函數,使用前需要確保在php編譯模塊時加入mbstring的支持:
(1)編譯時使用–enable-mbstring
(2)修改/usr/local/lib/php.inc
default_charset = “zh-cn”
mbstring.language = zh-cn
mbstring.internal_encoding =zh-cn
PHP字符串mbstring類庫內容比較多,還包括mb_ send_ mail 之類的email處理函數等
(100分)[php]寫幾個你熟悉的字符串處理函數!
推薦你查看官方PHP手冊,以下是摘取相關的資料:
addcslashes — 以 C 語言風格使用反斜線轉義字符串中的字符
addslashes — 使用反斜線引用字符串
bin2hex — 將二進制數據轉換成十六進制表示
chop — rtrim 的別名
chr — 返回指定的字符
chunk_split — 將字符串分割成小塊
convert_cyr_string — 將字符由一種 Cyrillic 字符轉換成另一種
convert_uudecode — 解碼一個 uuencode 編碼的字符串
convert_uuencode — 使用 uuencode 編碼一個字符串
count_chars — 返回字符串所用字符的信息
crc32 — 計算一個字符串的 crc32 多項式
crypt — 單向字符串散列
echo — 輸出一個或多個字符串
explode — 使用一個字符串分割另一個字符串
fprintf — 將格式化後的字符串寫入到流
get_html_translation_table — 返回使用 htmlspecialchars 和 htmlentities 後的轉換表
hebrev — 將邏輯順序希伯來文(logical-Hebrew)轉換為視覺順序希伯來文(visual-Hebrew)
hebrevc — 將邏輯順序希伯來文(logical-Hebrew)轉換為視覺順序希伯來文(visual-Hebrew),並且轉換換行符
hex2bin — Decodes a hexadecimally encoded binary string
html_entity_decode — Convert all HTML entities to their applicable characters
htmlentities — Convert all applicable characters to HTML entities
htmlspecialchars_decode — Convert special HTML entities back to characters
htmlspecialchars — Convert special characters to HTML entities
implode — Join array elements with a string
join — 別名 implode
lcfirst — Make a string’s first character lowercase
levenshtein — Calculate Levenshtein distance between two strings
localeconv — Get numeric formatting information
ltrim — Strip whitespace (or other characters) from the beginning of a string
md5_file — 計算指定文件的 MD5 散列值
md5 — 計算字符串的 MD5 散列值
metaphone — Calculate the metaphone key of a string
money_format — Formats a number as a currency string
nl_langinfo — Query language and locale information
nl2br — 在字符串所有新行之前插入 HTML 換行標記
number_format — Format a number with grouped thousands
ord — 返回字符的 ASCII 碼值
parse_str — 將字符串解析成多個變量
print — 輸出字符串
printf — 輸出格式化字符串
quoted_printable_decode — Convert a quoted-printable string to an 8 bit string
quoted_printable_encode — Convert a 8 bit string to a quoted-printable string
quotemeta — Quote meta characters
rtrim — 刪除字符串末端的空白字符(或者其他字符)
setlocale — Set locale information
sha1_file — 計算文件的 sha1 散列值
sha1 — 計算字符串的 sha1 散列值
similar_text — 計算兩個字符串的相似度
soundex — Calculate the soundex key of a string
sprintf — Return a formatted string
sscanf — Parses input from a string according to a format
str_getcsv — 解析 CSV 字符串為一個數組
str_ireplace — str_replace 的忽略大小寫版本
str_pad — 使用另一個字符串填充字符串為指定長度
str_repeat — 重複一個字符串
str_replace — 子字符串替換
str_rot13 — 對字符串執行 ROT13 轉換
str_shuffle — 隨機打亂一個字符串
str_split — 將字符串轉換為數組
str_word_count — 返回字符串中單詞的使用情況
strcasecmp — 二進制安全比較字符串(不區分大小寫)
strchr — 別名 strstr
strcmp — 二進制安全字符串比較
strcoll — 基於區域設置的字符串比較
strcspn — 獲取不匹配遮罩的起始子字符串的長度
strip_tags — 從字符串中去除 HTML 和 PHP 標記
stripcslashes — 反引用一個使用 addcslashes 轉義的字符串
stripos — 查找字符串首次出現的位置(不區分大小寫)
stripslashes — 反引用一個引用字符串
stristr — strstr 函數的忽略大小寫版本
strlen — 獲取字符串長度
strnatcasecmp — 使用“自然順序”算法比較字符串(不區分大小寫)
strnatcmp — 使用自然排序算法比較字符串
strncasecmp — 二進制安全比較字符串開頭的若干個字符(不區分大小寫)
strncmp — 二進制安全比較字符串開頭的若干個字符
strpbrk — 在字符串中查找一組字符的任何一個字符
strpos — 查找字符串首次出現的位置
strrchr — 查找指定字符在字符串中的最後一次出現
strrev — 反轉字符串
strripos — 計算指定字符串在目標字符串中最後一次出現的位置(不區分大小寫)
strrpos — 計算指定字符串在目標字符串中最後一次出現的位置
strspn — 計算字符串中全部字符都存在於指定字符集合中的第一段子串的長度。
strstr — 查找字符串的首次出現
strtok — 標記分割字符串
strtolower — 將字符串轉化為小寫
strtoupper — 將字符串轉化為大寫
strtr — 轉換指定字符
substr_compare — 二進制安全比較字符串(從偏移位置比較指定長度)
substr_count — 計算字串出現的次數
substr_replace — 替換字符串的子串
substr — 返回字符串的子串
trim — 去除字符串首尾處的空白字符(或者其他字符)
ucfirst — 將字符串的首字母轉換為大寫
ucwords — 將字符串中每個單詞的首字母轉換為大寫
vfprintf — 將格式化字符串寫入流
vprintf — 輸出格式化字符串
vsprintf — 返回格式化字符串
wordwrap — 打斷字符串為指定數量的字串
====================
這其中我熟悉的有:
echo — 輸出一個或多個字符串
explode — 使用一個字符串分割另一個字符串
addslashes — 使用反斜線引用字符串
bin2hex — 將二進制數據轉換成十六進制表示
html_entity_decode — Convert all HTML entities to their applicable characters
htmlentities — Convert all applicable characters to HTML entities
htmlspecialchars_decode — Convert special HTML entities back to characters
htmlspecialchars — Convert special characters to HTML entities
implode — Join array elements with a string
……….
大部分都用過
php去除字符串中空字符的常用方法
關於php去除字符串中空字符的常用方法
本文實例總結了php去除字符串中空字符的’常用方法。分享給大家供大家參考。具體分析如下:
php中包含四個可以去除字符串空格的函數:
trim() – 去除字符串兩端的空字符
ltrim() – 去除字符串前端的空字符
rtrim() – 去除字符串末尾的空字符
chop() –同rtrim().
代碼如下:
“t t !t t “;$leftTrimmed = ltrim($text);$rightTrimmed = rtrim($text);$bothTrimmed = trim($text);print(“leftTrimmed = ($leftTrimmed)n”);print(“rightTrimmed = ($rightTrimmed)n”);print(“bothTrimmed = ($bothTrimmed)n”);?
輸出結果:
leftTrimmed = (! )rightTrimmed = ( !)bothTrimmed = (!)
希望本文所述對大家的php程序設計有所幫助。
;
求字符串處理函數(全)
函數名: stpcpy
功 能: 拷貝一個字符串到另一個
用 法: char *stpcpy(char *destin, char *source);
程序例:
#include stdio.h
#include string.h
int main(void)
{
char string[10];
char *str1 = “abcdefghi”;
stpcpy(string, str1);
printf(“%sn”, string);
return 0;
}
函數名: strcat
功 能: 字符串拼接函數
用 法: char *strcat(char *destin, char *source);
程序例:
#include string.h
#include stdio.h
int main(void)
{
char destination[25];
char *blank = ” “, *c = “C++”, *Borland = “Borland”;
strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c);
printf(“%sn”, destination);
return 0;
}
函數名: strchr
功 能: 在一個串中查找給定字符的第一個匹配之處
用 法: char *strchr(char *str, char c);
程序例:
#include string.h
#include stdio.h
int main(void)
{
char string[15];
char *ptr, c = ‘r’;
strcpy(string, “This is a string”);
ptr = strchr(string, c);
if (ptr)
printf(“The character %c is at position: %dn”, c, ptr-string);
else
printf(“The character was not foundn”);
return 0;
}
函數名: strcmp
功 能: 串比較
用 法: int strcmp(char *str1, char *str2);
看Asic碼,str1str2,返回值 0;兩串相等,返回0
程序例:
#include string.h
#include stdio.h
int main(void)
{
char *buf1 = “aaa”, *buf2 = “bbb”, *buf3 = “ccc”;
int ptr;
ptr = strcmp(buf2, buf1);
if (ptr 0)
printf(“buffer 2 is greater than buffer 1n”);
else
printf(“buffer 2 is less than buffer 1n”);
ptr = strcmp(buf2, buf3);
if (ptr 0)
printf(“buffer 2 is greater than buffer 3n”);
else
printf(“buffer 2 is less than buffer 3n”);
return 0;
}
函數名: strncmpi
功 能: 將一個串中的一部分與另一個串比較, 不管大小寫
用 法: int strncmpi(char *str1, char *str2, unsigned maxlen);
程序例:
#include string.h
#include stdio.h
int main(void)
{
char *buf1 = “BBB”, *buf2 = “bbb”;
int ptr;
ptr = strcmpi(buf2, buf1);
if (ptr 0)
printf(“buffer 2 is greater than buffer 1n”);
if (ptr 0)
printf(“buffer 2 is less than buffer 1n”);
if (ptr == 0)
printf(“buffer 2 equals buffer 1n”);
return 0;
}
函數名: strcpy
功 能: 串拷貝
用 法: char *strcpy(char *str1, char *str2);
程序例:
#include stdio.h
#include string.h
int main(void)
{
char string[10];
char *str1 = “abcdefghi”;
strcpy(string, str1);
printf(“%sn”, string);
return 0;
}
函數名: strcspn
功 能: 在串中查找第一個給定字符集內容的段
用 法: int strcspn(char *str1, char *str2);
程序例:
#include stdio.h
#include string.h
#include alloc.h
int main(void)
{
char *string1 = “1234567890”;
char *string2 = “747DC8”;
int length;
length = strcspn(string1, string2);
printf(“Character where strings intersect is at position %dn”, length);
return 0;
}
函數名: strdup
功 能: 將串拷貝到新建的位置處
用 法: char *strdup(char *str);
程序例:
#include stdio.h
#include string.h
#include alloc.h
int main(void)
{
char *dup_str, *string = “abcde”;
dup_str = strdup(string);
printf(“%sn”, dup_str);
free(dup_str);
return 0;
}
函數名: stricmp
功 能: 以大小寫不敏感方式比較兩個串
用 法: int stricmp(char *str1, char *str2);
程序例:
#include string.h
#include stdio.h
int main(void)
{
char *buf1 = “BBB”, *buf2 = “bbb”;
int ptr;
ptr = stricmp(buf2, buf1);
if (ptr 0)
printf(“buffer 2 is greater than buffer 1n”);
if (ptr 0)
printf(“buffer 2 is less than buffer 1n”);
if (ptr == 0)
printf(“buffer 2 equals buffer 1n”);
return 0;
}
函數名: strerror
功 能: 返回指向錯誤信息字符串的指針
用 法: char *strerror(int errnum);
程序例:
#include stdio.h
#include errno.h
int main(void)
{
char *buffer;
buffer = strerror(errno);
printf(“Error: %sn”, buffer);
return 0;
}
函數名: strcmpi
功 能: 將一個串與另一個比較, 不管大小寫
用 法: int strcmpi(char *str1, char *str2);
程序例:
#include string.h
#include stdio.h
int main(void)
{
char *buf1 = “BBB”, *buf2 = “bbb”;
int ptr;
ptr = strcmpi(buf2, buf1);
if (ptr 0)
printf(“buffer 2 is greater than buffer 1n”);
if (ptr 0)
printf(“buffer 2 is less than buffer 1n”);
if (ptr == 0)
printf(“buffer 2 equals buffer 1n”);
return 0;
}
函數名: strncmp
功 能: 串比較
用 法: int strncmp(char *str1, char *str2, int maxlen);
程序例:
#include string.h
#include stdio.h
int main(void)
{
char *buf1 = “aaabbb”, *buf2 = “bbbccc”, *buf3 = “ccc”;
int ptr;
ptr = strncmp(buf2,buf1,3);
if (ptr 0)
printf(“buffer 2 is greater than buffer 1n”);
else
printf(“buffer 2 is less than buffer 1n”);
ptr = strncmp(buf2,buf3,3);
if (ptr 0)
printf(“buffer 2 is greater than buffer 3n”);
else
printf(“buffer 2 is less than buffer 3n”);
return(0);
}
函數名: strncmpi
功 能: 把串中的一部分與另一串中的一部分比較, 不管大小寫
用 法: int strncmpi(char *str1, char *str2);
程序例:
#include string.h
#include stdio.h
int main(void)
{
char *buf1 = “BBBccc”, *buf2 = “bbbccc”;
int ptr;
ptr = strncmpi(buf2,buf1,3);
if (ptr 0)
printf(“buffer 2 is greater than buffer 1n”);
if (ptr 0)
printf(“buffer 2 is less than buffer 1n”);
if (ptr == 0)
printf(“buffer 2 equals buffer 1n”);
return 0;
}
函數名: strncpy
功 能: 串拷貝
用 法: char *strncpy(char *destin, char *source, int maxlen);
程序例:
#include stdio.h
#include string.h
int main(void)
{
char string[10];
char *str1 = “abcdefghi”;
strncpy(string, str1, 3);
string[3] = ”;
printf(“%sn”, string);
return 0;
}
函數名: strnicmp
功 能: 不注重大小寫地比較兩個串
用 法: int strnicmp(char *str1, char *str2, unsigned maxlen);
程序例:
#include string.h
#include stdio.h
int main(void)
{
char *buf1 = “BBBccc”, *buf2 = “bbbccc”;
int ptr;
ptr = strnicmp(buf2, buf1, 3);
if (ptr 0)
printf(“buffer 2 is greater than buffer 1n”);
if (ptr 0)
printf(“buffer 2 is less than buffer 1n”);
if (ptr == 0)
printf(“buffer 2 equals buffer 1n”);
return 0;
}
函數名: strnset
功 能: 將一個串中的所有字符都設為指定字符
用 法: char *strnset(char *str, char ch, unsigned n);
程序例:
#include stdio.h
#include string.h
int main(void)
{
char *string = “abcdefghijklmnopqrstuvwxyz”;
char letter = ‘x’;
printf(“string before strnset: %sn”, string);
strnset(string, letter, 13);
printf(“string after strnset: %sn”, string);
return 0;
}
函數名: strpbrk
功 能: 在串中查找給定字符集中的字符
用 法: char *strpbrk(char *str1, char *str2);
程序例:
#include stdio.h
#include string.h
int main(void)
{
char *string1 = “abcdefghijklmnopqrstuvwxyz”;
char *string2 = “onm”;
char *ptr;
ptr = strpbrk(string1, string2);
if (ptr)
printf(“strpbrk found first character: %cn”, *ptr);
else
printf(“strpbrk didn’t find character in setn”);
return 0;
}
函數名: strrchr
功 能: 在串中查找指定字符的最後一個出現
用 法: char *strrchr(char *str, char c);
程序例:
#include string.h
#include stdio.h
int main(void)
{
char string[15];
char *ptr, c = ‘r’;
strcpy(string, “This is a string”);
ptr = strrchr(string, c);
if (ptr)
printf(“The character %c is at position: %dn”, c, ptr-string);
else
printf(“The character was not foundn”);
return 0;
}
函數名: strrev
功 能: 串倒轉
用 法: char *strrev(char *str);
程序例:
#include string.h
#include stdio.h
int main(void)
{
char *forward = “string”;
printf(“Before strrev(): %sn”, forward);
strrev(forward);
printf(“After strrev(): %sn”, forward);
return 0;
}
函數名: strset
功 能: 將一個串中的所有字符都設為指定字符
用 法: char *strset(char *str, char c);
程序例:
#include stdio.h
#include string.h
int main(void)
{
char string[10] = “123456789”;
char symbol = ‘c’;
printf(“Before strset(): %sn”, string);
strset(string, symbol);
printf(“After strset(): %sn”, string);
return 0;
}
函數名: strspn
功 能: 在串中查找指定字符集的子集的第一次出現
用 法: int strspn(char *str1, char *str2);
程序例:
#include stdio.h
#include string.h
#include alloc.h
int main(void)
{
char *string1 = “1234567890”;
char *string2 = “123DC8”;
int length;
length = strspn(string1, string2);
printf(“Character where strings differ is at position %dn”, length);
return 0;
}
函數名: strstr
功 能: 在串中查找指定字符串的第一次出現
用 法: char *strstr(char *str1, char *str2);
程序例:
#include stdio.h
#include string.h
int main(void)
{
char *str1 = “Borland International”, *str2 = “nation”, *ptr;
ptr = strstr(str1, str2);
printf(“The substring is: %sn”, ptr);
return 0;
}
函數名: strtod
功 能: 將字符串轉換為double型值
用 法: double strtod(char *str, char **endptr);
程序例:
#include stdio.h
#include stdlib.h
int main(void)
{
char input[80], *endptr;
double value;
printf(“Enter a floating point number:”);
gets(input);
value = strtod(input, endptr);
printf(“The string is %s the number is %lfn”, input, value);
return 0;
}
函數名: strtok
功 能: 查找由在第二個串中指定的分界符分隔開的單詞
用 法: char *strtok(char *str1, char *str2);
程序例:
#include string.h
#include stdio.h
int main(void)
{
char input[16] = “abc,d”;
char *p;
/* strtok places a NULL terminator
in front of the token, if found */
p = strtok(input, “,”);
if (p) printf(“%sn”, p);
/* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok(NULL, “,”);
if (p) printf(“%sn”, p);
return 0;
}
函數名: strtol
功 能: 將串轉換為長整數
用 法: long strtol(char *str, char **endptr, int base);
程序例:
#include stdlib.h
#include stdio.h
int main(void)
{
char *string = “87654321”, *endptr;
long lnumber;
/* strtol converts string to long integer */
lnumber = strtol(string, endptr, 10);
printf(“string = %s long = %ldn”, string, lnumber);
return 0;
}
函數名: strupr
功 能: 將串中的小寫字母轉換為大寫字母
用 法: char *strupr(char *str);
程序例:
#include stdio.h
#include string.h
int main(void)
{
char *string = “abcdefghijklmnopqrstuvwxyz”, *ptr;
/* converts string to upper case characters */
ptr = strupr(string);
printf(“%sn”, ptr);
return 0;
}
函數名: swab
功 能: 交換字節
用 法: void swab (char *from, char *to, int nbytes);
程序例:
#include stdlib.h
#include stdio.h
#include string.h
char source[15] = “rFna koBlrna d”;
char target[15];
int main(void)
{
swab(source, target, strlen(source));
printf(“This is target: %sn”, target);
return 0;
}
PS:isalpha()是字符函數,不是字符串函數,
isalpha
原型:extern int isalpha(int c);
用法:#include ctype.h
功能:判斷字符c是否為英文字母
說明:當c為英文字母a-z或A-Z時,返回非零值,否則返回零。
舉例:
// isalpha.c
#include syslib.h
#include ctype.h
#include stdio.h
main()
{
int c;
clrscr(); // clear screen
printf(“Press a key”);
for(;;)
{
c=getchar();
clrscr();
printf(“%c: %s letter”,c,isalpha(c)?”is”:”not”);
}
return 0; // just to avoid warnings by compiler
}
PHP字符串如何替換函數???
原字符串中的所有”iwind”都替換成了”kiki”.str_replace是大小寫敏感的,所以對你不能設想用 str_replace(“iwind”, “kiki”,…)替換原字符串中的”iwind”. str_replace還可以實現多對一
定義和用法
str_replace() 函數使用一個字符串替換字符串中的另一些字符。
語法
str_replace(find,replace,string,count)參數 描述
find 必需。規定要查找的值。
replace 必需。規定替換 find 中的值的值。
string 必需。規定被搜索的字符串。
count 可選。一個變量,對替換數進行計數。
下面用一款
//– 程序名稱:strreplace()
//– 程序用途:替換變量中的非法字符
//– 傳入參數:變量值
//********************************************************
function strreplace($str){
$str = strips教程lashes($str);
$str = str_replace(chr(92),”,$str);
$str = str_replace(chr(47),”,$str);
$str = str_replace(chr(10).chr(13),”br”,$str);
$str = str_replace(”,””,$str);
$str = str_replace(”,””,$str);
$str = str_replace(‘;’,”;”,$str);
$str = str_replace(‘”‘,”“”,$str);
$str = str_replace(“‘”,”‘”,$str);
$str = str_replace(” “,” “,$str);
$str = str_replace(“/**/”,” “,$str);
return trim($str);
}
php哪個函數能取得字符串長度
PHP對中文字符串的處理一直困擾於剛剛接觸PHP開發的新手程序員。下面簡要的剖析一下PHP對中文字符串長度的處理:
PHP自帶的函數如strlen()、mb_strlen()都是通過計算字符串所佔字節數來統計字符串長度的,一個英文字符佔1字節。
原創文章,作者:WCWVD,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/316308.html