Common string processing code snippets

Source: Internet
Author: User
Remove the HTML tag $ text = strip_tags ($ input,); returns the text functionGetBetween ($ content, $ start, $ end) between $ start and $ end) {$ r = explode ($ start, $ content); if (isse

  Remove HTML tags

 
 
  1. $text = strip_tags($input, ""); 
   Returns the text between $ start and $ end.
 
 
  1. function GetBetween($content,$start,$end){ 
  2.     $r = explode($start, $content); 
  3.     if (isset($r[1])){ 
  4.         $r = explode($end, $r[1]); 
  5.         return $r[0]; 
  6.     } 
  7.     return ''; 
   Convert a url to a link
 
 
  1. $url = "Jean-Baptiste Jung (http://www.webdevcat.com)"; 
  2. $url = preg_replace("#http://([A-z0-9./-]+)#", '$0', $url); 
   Split the string to 140 characters
 
 
  1. function split_to_chunks($to,$text){ 
  2.     $total_length = (140 - strlen($to)); 
  3.     $text_arr = explode(" ",$text); 
  4.     $i=0; 
  5.     $message[0]=""; 
  6.     foreach ($text_arr as $word){ 
  7.         if ( strlen($message[$i] . $word . ' ') <= $total_length ){ 
  8.             if ($text_arr[count($text_arr)-1] == $word){ 
  9.                 $message[$i] .= $word; 
  10.             } else { 
  11.                 $message[$i] .= $word . ' '; 
  12.             } 
  13.         } else { 
  14.             $i++; 
  15.             if ($text_arr[count($text_arr)-1] == $word){ 
  16.                 $message[$i] = $word; 
  17.             } else { 
  18.                 $message[$i] = $word . ' '; 
  19.             } 
  20.         } 
  21.     } 
  22.     return $message; 

  Delete a URL from a string

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

  Convert string to SEO-friendly string

 
 
  1. function slug($str){ 
  2.     $str = strtolower(trim($str)); 
  3.     $str = preg_replace('/[^a-z0-9-]/', '-', $str); 
  4.     $str = preg_replace('/-+/', "-", $str); 
  5.     return $str; 

  Parse CSV files

 
 
  1. $fh = fopen("contacts.csv", "r"); 
  2. while($line = fgetcsv($fh, 1000, ",")) { 
  3.     echo "Contact: {$line[1]}"; 
   String search
 
 
  1. function contains($str, $content, $ignorecase=true){ 
  2.     if ($ignorecase){ 
  3.         $str = strtolower($str); 
  4.         $content = strtolower($content); 
  5.     } 
  6.     return strpos($content,$str) ? true : false; 
   Check whether the string starts with a string
 
 
  1. function String_Begins_With($needle, $haystack { 
  2.     return (substr($haystack, 0, strlen($needle))==$needle); 
   Extract the email address from the string
 
 
  1. function extract_emails($str){ 
  2.     // This regular expression extracts all emails from a string: 
  3.     $regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i'; 
  4.     preg_match_all($regexp, $str, $m); 
  5.  
  6.     return isset($m[0]) ? $m[0] : array(); 
  7.  
  8. $test_string = 'This is a test string... 
  9.  
  10.         test1@example.org 
  11.  
  12.         Test different formats: 
  13.         test2@example.org; 
  14.         "test3@example.org">foobar 
  15.           
  16.  
  17.         strange formats: 
  18.         test5@example.org 
  19.         test6[at]example.org 
  20.         test7@example.net.org.com 
  21.         test8@ example.org 
  22.         test9@!foo!.org 
  23.  
  24.         foobar 
  25. '; 
  26.  
  27. print_r(extract_emails($test_string)); 
   [PHP] code
 
 
  1. function extract_emails($str){ 
  2.     // This regular expression extracts all emails from a string: 
  3.     $regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i'; 
  4.     preg_match_all($regexp, $str, $m); 
  5.  
  6.     return isset($m[0]) ? $m[0] : array(); 
  7.  
  8. $test_string = 'This is a test string... 
  9.  
  10.         test1@example.org 
  11.  
  12.         Test different formats: 
  13.         test2@example.org; 
  14.         "test3@example.org">foobar 
  15.           
  16.  
  17.         strange formats: 
  18.         test5@example.org 
  19.         test6[at]example.org 
  20.         test7@example.net.org.com 
  21.         test8@ example.org 
  22.         test9@!foo!.org 
  23.  
  24.         foobar 
  25. '; 
  26.  
  27. print_r(extract_emails($test_string)); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.