龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

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

时间:2014-07-22 14:51来源: 作者: 点击:
分享到:
<无详细内容>
$text = strip_tags($input, "");

2. [代码]返回 $start 和 $end 之间的文本     跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]

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

3. [代码]将url转换成链接     跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]

$url = "Jean-Baptiste Jung (http://www.webdevcat.com)";
$url = preg_replace("#http://([A-z0-9./-]+)#", '<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>', $url);

4. [代码]切分字符串为140个字符     跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]

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 . ' ') <= $total_length ){
			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;
}

5. [代码]删除字符串中的URL     跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]

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

6. [代码]将字符串转成SEO友好的字符串     跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]

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

7. [代码]解析 CSV 文件     跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]

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

8. [代码]字符串搜索     跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]

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

9. [代码]检查字符串是否以某个串开始     跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]

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

10. [代码]从字符串中提取email地址     跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]

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;
        <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
';

print_r(extract_emails($test_string));

11. [代码][PHP]代码     跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]

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;
        <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
';

print_r(extract_emails($test_string));
精彩图集

赞助商链接