Common string processing code snippets in PHP _php tutorial

Source: Internet
Author: User
Remove HTML Tags
Copy CodeThe code is as follows:
$text = Strip_tags ($input, "");

The above function is mainly used strip_tags, the specific use of the instructions reference.
Returns the text between the $start and the $end
Copy CodeThe 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 URLs to Links
Copy CodeThe code is as follows:
$url = "Jean-baptiste Jung (http://www.jb51.net)";
$url = Preg_replace ("#http://([a-z0-9./-]+) #", ' $ A ', $url);

The Shard string is 140 characters
Copy CodeThe 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;
}

Remove a URL from a string
Copy CodeThe code is as follows:
$string = Preg_replace ('/\b (https?| Ftp|file): \/\/[-a-z0-9+&@#\/%?=~_|$!:,.;] *[a-z0-9+&@#\/%=~_|$]/i ', ', $string);

Convert string to SEO friendly string
Copy CodeThe code is as follows:
function slug ($str) {
$str = Strtolower (Trim ($STR));
$str = preg_replace ('/[^a-z0-9-]/', '-', $str);
$str = preg_replace ('/-+/', "-", $str);
return $str;
}

Parse CSV file
Copy CodeThe code is as follows:
$fh = fopen ("Contacts.csv", "R");
while ($line = Fgetcsv ($fh, 1000, ",")) {
echo "Contact: {$line [1]}";
}

String search
Copy CodeThe code is as follows:
function contains ($STR, $content, $ignorecase =true) {
if ($ignorecase) {
$str = Strtolower ($STR);
$content = Strtolower ($content);
}
Return Strpos ($content, $str)? True:false;
}

Checks if a string begins
Copy CodeThe code is as follows:
function String_begins_with ($needle, $haystack {
Return (substr ($haystack, 0, strlen ($needle)) = = $needle);
}

Extract the email address from the string
Copy CodeThe code is as follows:
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;
Foobar


Strange formats:
test5@example.org
test6[at]example.org
Test7@example.ne t.org.com
test8@ example.org
Test9@!foo!. ORG

foobar
';

Print_r (extract_emails ($test _string));

http://www.bkjia.com/phpjc/324426.html Span id= "Indexurl" itemprop= "Indexurl" >www.bkjia.com true http://www.bkjia.com/phpjc/324426.html techarticle remove HTML tag copy code code as follows: $text = Strip_tags ($input, ""); The above function is mainly used strip_tags, the specific use of the instructions reference. Return between $start and $end ...

  • 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.