Table of Contents
2. [代码]返回 $start 和 $end 之间的文本    
3. [代码]将url转换成链接    
4. [代码]切分字符串为140个字符    
5. [代码]删除字符串中的URL    
6. [代码]将字符串转成SEO友好的字符串    
7. [代码]解析 CSV 文件   
8. [代码]字符串搜索    
9. [代码]检查字符串是否以某个串开始  
10. [代码]从字符串中提取email地址    
11. [代码][PHP]代码    
Home php教程 PHP源码 10 个 PHP 关于字符串处理的代码片段

10 个 PHP 关于字符串处理的代码片段

May 25, 2016 pm 05:12 PM

代码片段(11) [全屏查看所有代码]

                       

                       

1. [代码]移除 HTML 标签  

           

$text = strip_tags($input, "");
Copy after login

2. [代码]返回 $start 和 $end 之间的文本

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

3. [代码]将url转换成链接

$url = "Jean-Baptiste Jung (http://www.php.cn/)";
$url = preg_replace("#http://([A-z0-9./-]+)#", &#39;<a href="http://www.catswhocode.com/blog/$1" style="font-size: 12px; vertical-align: baseline; background-color: transparent; margin: 0px; padding: 0px; color: #3777af; text-decoration: none; font-weight: bold">$0</a>&#39;, $url);
Copy after login

4. [代码]切分字符串为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 . &#39; &#39;) <= $total_length ){
			if ($text_arr[count($text_arr)-1] == $word){
				$message[$i] .= $word;
			} else {
				$message[$i] .= $word . &#39; &#39;;
			}
		} else {
			$i++;
			if ($text_arr[count($text_arr)-1] == $word){
				$message[$i] = $word;
			} else {
				$message[$i] = $word . &#39; &#39;;
			}
		}
	}
	return $message;
}
Copy after login

5. [代码]删除字符串中的URL

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

6. [代码]将字符串转成SEO友好的字符串

function slug($str){
	$str = strtolower(trim($str));
	$str = preg_replace(&#39;/[^a-z0-9-]/&#39;, &#39;-&#39;, $str);
	$str = preg_replace(&#39;/-+/&#39;, "-", $str);
	return $str;
}
Copy after login

7. [代码]解析 CSV 文件

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

8. [代码]字符串搜索

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

9. [代码]检查字符串是否以某个串开始

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

10. [代码]从字符串中提取email地址

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

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

$test_string = &#39;This is a test string...

        test1@example.org

        Test different formats:
        test2@example.org;
        <a href="test3@example.org">foobar</a>
        <test4@example.org>

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

        foobar
&#39;;

print_r(extract_emails($test_string));
Copy after login
Copy after login

11. [代码][PHP]代码

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

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

$test_string = &#39;This is a test string...

        test1@example.org

        Test different formats:
        test2@example.org;
        <a href="test3@example.org">foobar</a>
        <test4@example.org>

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

        foobar
&#39;;

print_r(extract_emails($test_string));
Copy after login
Copy after login

                   

                   

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24