Organizing common string processing code snippets in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:23:52
Original
1034 people have browsed it

Remove HTML tags

Copy code The code is as follows:

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

The above function mainly uses strip_tags, please refer to the specific usage instructions.
Return the text between $start and $end
Copy code The code is as follows:

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

Convert url to link
Copy code The code is as follows:

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

Split the string into 140 characters
Copy code The code is as follows:

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

Delete the URL in the string
Copy code The code is as follows :

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

Convert the string to SEO friendly String of
Copy code The code is as follows:

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

Parsing CSV files
Copy code The code is as follows:

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

 String search
Copy code The code is as follows:

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

  Check characters Whether the string starts with a certain string
Copy code The code is as follows:

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

 Extract email address from string
Copy code The code is as follows:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324426.htmlTechArticleRemove HTML tags and copy the code as follows: $text = strip_tags($input, ""); The above function Mainly using strip_tags, please refer to the specific usage instructions. Returns between $start and $end...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template