Home Backend Development PHP Tutorial php中常用字符串处理代码片段整理_php技巧

php中常用字符串处理代码片段整理_php技巧

May 17, 2016 am 09:14 AM
String processing

移除 HTML 标签

复制代码 代码如下:

$text = strip_tags($input, "");

上面的函数主要是使用了strip_tags,具体的使用说明参考。
  返回 $start 和 $end 之间的文本
复制代码 代码如下:

function GetBetween($content,$start,$end){
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}

  将url转换成链接
复制代码 代码如下:

$url = "Jean-Baptiste Jung (http://www.jb51.net)";
$url = preg_replace("#http://([A-z0-9./-]+)#", '$0', $url);

  切分字符串为140个字符
复制代码 代码如下:

function split_to_chunks($to,$text){
$total_length = (140 - strlen($to));
$text_arr = explode(" ",$text);
$i=0;
$message[0]="";
foreach ($text_arr as $word){
if ( strlen($message[$i] . $word . ' ') if ($text_arr[count($text_arr)-1] == $word){
$message[$i] .= $word;
} else {
$message[$i] .= $word . ' ';
}
} else {
$i++;
if ($text_arr[count($text_arr)-1] == $word){
$message[$i] = $word;
} else {
$message[$i] = $word . ' ';
}
}
}
return $message;
}

  删除字符串中的URL
复制代码 代码如下:

$string = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $string);

  将字符串转成SEO友好的字符串
复制代码 代码如下:

function slug($str){
$str = strtolower(trim($str));
$str = preg_replace('/[^a-z0-9-]/', '-', $str);
$str = preg_replace('/-+/', "-", $str);
return $str;
}

  解析 CSV 文件
复制代码 代码如下:

$fh = fopen("contacts.csv", "r");
while($line = fgetcsv($fh, 1000, ",")) {
echo "Contact: {$line[1]}";
}

  字符串搜索
复制代码 代码如下:

function contains($str, $content, $ignorecase=true){
if ($ignorecase){
$str = strtolower($str);
$content = strtolower($content);
}
return strpos($content,$str) ? true : false;
}

  检查字符串是否以某个串开始
复制代码 代码如下:

function String_Begins_With($needle, $haystack {
return (substr($haystack, 0, strlen($needle))==$needle);
}

  从字符串中提取email地址
复制代码 代码如下:

function extract_emails($str){
// This regular expression extracts all emails from a string:
$regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
preg_match_all($regexp, $str, $m);

return isset($m[0]) ? $m[0] : array();
}

$test_string = 'This is a test string...

test1@example.org

Test different formats:
test2@example.org;
foobar


strange formats:
test5@example.org
test6[at]example.org
test7@example.net.org.com
test8@ example.org
test9@!foo!.org

foobar
';

print_r(extract_emails($test_string));
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain in simple terms: Detailed explanation of string escaping and anti-escaping in GO language Explain in simple terms: Detailed explanation of string escaping and anti-escaping in GO language Apr 07, 2024 am 10:39 AM

In Go language, string escape uses backslash (\`) plus special characters to represent special characters, such as newline character (\n). Anti-escaping uses backticks (\`) to remove escaped characters and restore their original characters, such as \n representing the actual newline character. Practical cases demonstrate the application of escaping, anti-escaping and anti-escaping in file reading.

Errors that may occur in PHP string processing and how to fix them Errors that may occur in PHP string processing and how to fix them May 11, 2023 pm 05:21 PM

PHP is a widely used dynamic programming language that has a wide range of applications, especially in the development of web applications. String processing is one of the most commonly used functions in PHP, but many times developers encounter various errors and problems when processing strings. In this article, we will explore several common problems you may encounter during PHP string processing and how to solve them. Character Encoding Issues When processing strings, a common issue is character encoding. There are many different character encodings, the most common of which is UT

Best practices for converting strings to floating point numbers in PHP Best practices for converting strings to floating point numbers in PHP Mar 28, 2024 am 08:18 AM

Converting strings to floating point numbers in PHP is a common requirement during the development process. For example, the amount field read from the database is of string type and needs to be converted into floating point numbers for numerical calculations. In this article, we will introduce the best practices for converting strings to floating point numbers in PHP and give specific code examples. First of all, we need to make it clear that there are two main ways to convert strings to floating point numbers in PHP: using (float) type conversion or using (floatval) function. Below we will introduce these two

Master regular expressions and string processing in Go language Master regular expressions and string processing in Go language Nov 30, 2023 am 09:54 AM

As a modern programming language, Go language provides powerful regular expressions and string processing functions, allowing developers to process string data more efficiently. It is very important for developers to master regular expressions and string processing in Go language. This article will introduce in detail the basic concepts and usage of regular expressions in Go language, and how to use Go language to process strings. 1. Regular expressions Regular expressions are a tool used to describe string patterns. They can easily implement operations such as string matching, search, and replacement.

Master the secrets of string escaping and anti-escaping in GO language Master the secrets of string escaping and anti-escaping in GO language Apr 07, 2024 pm 04:33 PM

String escaping uses backslashes to represent special characters as escape sequences, while unescaping returns escape sequences to actual characters. The Go language supports the following escape sequences: \n (line feed), \t (tab), \r (carriage return), \f (form feed), \a (alarm), \b (backspace) ), \v (vertical tab), in addition to the backslash itself, single quotes, and double quotes. Raw string literals are enclosed in backticks and no characters are escaped. Escape characters are useful in HTML code and JSON data to display or escape special characters.

PHP string processing: detailed explanation of how to remove all spaces PHP string processing: detailed explanation of how to remove all spaces Mar 23, 2024 pm 06:51 PM

PHP is a powerful programming language that is widely used in web development. In the process of web development, we often encounter situations where strings need to be processed, and removing spaces from strings is a common requirement. This article will introduce in detail how to remove all spaces from a string in PHP and provide specific code examples. 1. Use the str_replace function. The str_replace function is a commonly used string replacement function in PHP. It can replace a specified character with another character. By using this function, you can

How to remove specific characters from a string in PHP using regular expressions How to remove specific characters from a string in PHP using regular expressions Jun 22, 2023 pm 03:46 PM

In PHP, you can easily remove specific characters from a string using regular expressions. Regular expression is a powerful tool that helps us match and manipulate text based on specified patterns. In this article, we will introduce how to use regular expressions to remove specific characters from a string, and how to use the preg_replace function in PHP to achieve this goal. Using regular expressions to replace specific characters "." in regular expressions identifies any single character, we can use

What are the 7 PHP string processing functions? What are the 7 PHP string processing functions? Sep 18, 2023 pm 02:14 PM

The seven PHP string processing functions include strlen(), strpos(), substr(), str_replace(), strtolower(), strtoupper(), trim(), etc. Detailed introduction: 1. strlen(), used to obtain the length of a string; 2. strpos(), used to find a specific substring in a string and return the first occurrence position; 3. substr(), used to obtain Substring of string; 4. str_replace(), etc.

See all articles