10 string-related PHP code Snippets

Source: Internet
Author: User
Tags explode wordpress blog
1. Automatically remove HTML tags from stringsIn the user form, you may want to remove all unnecessary HTML tags. You can do this simply by using the Strip_tags () function: $text = strip_tags ($input, ""); 22, get text between $start and $end This is a feature that every web developer should be able to store in the development Toolbox: given a string, A starting position, a end, and the text that is contained between the $start and $end. function Getbetween ($content, $start, $end) {
$r = Explode ($start, $content);
if (Isset ($r [1])) {
$r = Explode ($end, $r [1]);
return $r [0];
}
Return ";
}3, convert URLs to hyperlinks If you add a URL to your WordPress blog's comment form, it will be automatically converted to a hyperlink. If you want to implement the same functionality on your site, you can use the following code: $URL = "Server Operations and Architecture (http://www.ha97.com)";
$url = Preg_replace ("#http://([a-z0-9./-]+) #", ' $0′, $url);

4. Divide text into 140-character arrays as we all know, Twitter only accepts messages within 140 characters. If you want to interact with this popular instant messaging site, you will certainly like this feature, which will allow the message to be truncated to 140 characters. 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, removing URLs from a string in order to get traffic or backlinks, many visitors post a lot of blog comments that contain URL information that can be effectively protected: $string = Preg_replace ('/b (https?| Ftp|file)://[-a-z0-9+&@#/%?=~_|$!:,.;] *[a-z0-9+&@#/%=~_|$]/i ', ", $string); 6, the conversion string for the thumbnail title to create a thumbnail title (often referred to as permalink, that is, fixed link) for SEO, the following function with a string as a parameter, and returns a good abbreviated string. Simple and effective, worth trying! function slug ($str) {
$str = Strtolower (Trim ($STR));
$str = preg_replace ('/[^a-z0-9-]/', '-', $str);
$str = preg_replace ('/-+/', "-", $str);
return $str;
}7, parsing csv file csv (Comma separated value file) is an easy way to store data, and parsing with PHP is easy. Do not believe you can try the following code snippet. $fh = fopen ("Contacts.csv", "R");
while ($line = Fgetcsv ($fh, 1000, ",")) {
echo "Contact: {$line [1]}";
}18, retrieving another string in a string if a string is contained in another string and must be retrieved, here's a wonderful way: function contains ($STR, $content, $ignorecase =true) {
if ($ignorecase) {
$str = Strtolower ($STR);
$content = Strtolower ($content);
}
Return Strpos ($content, $str)? True:false;
}9, detects whether a string starts in a specified pattern some languages such as Java have a Startwith method that allows you to detect whether a string starts in the specified pattern. Unfortunately, PHP does not have this built-in function. But we can do it ourselves. The implementation is simple: function String_begins_with ($needle, $haystack {
Return (substr ($haystack, 0, strlen ($needle)) = = $needle);
}10, extracting an e-mail address from a string have you ever thought about how people who send spam get their e-mail addresses? This is simple, they simply parse the Web page to extract the e-mail messages in simple HTML. This code requires a string as an argument and prints the e-mail address it contains. Caution: Do not use this code to make junk mail! 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.orgtest 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!.orgfoobar
';p Rint_r (extract_emails ($test _string)); Like my article, then pay attention to my toceansoft.
  • 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.