PHP str_replace function (can limit the number of replacements) _php tutorial

Source: Internet
Author: User
PHP str_replace function (can limit the number of replacements) This article mainly introduces the Str_replace function to replace a single character, and replace the data, as well as specify the number of Str_replace replacements, the last replacement number is very useful especially in SEO optimization method surface Oh.

PHP Tutorial Str_replace function (can limit the number of replacements)
This article mainly introduces the Str_replace function to replace a single character, with the replacement data, as well as specifying the number of Str_replace replacements, the last replacement count is useful especially in SEO Tutorial optimization method Surface Oh.
/mixed str_replace (mixed $search, mixed $replace, mixed $subject [, int & $count])

A function returns a string or replaces the given array substitution value in all occurrences of the problem.

If you don't like rules that need to be replaced (such as regular expressions), you should always use this function instead of the ereg_replace () or Preg_replace function ().
*/
Provides:


$bodytag = Str_replace ("%body%", "Black", "" ");

Provides:hll Wrld F PHP
$vowels = Array ("A", "E", "I", "O", "U", "a", "E", "I", "O", "U");
$onlyconsonants = Str_replace ($vowels, "", "Hello World of PHP");

Provides:you should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = Array ("Fruits", "vegetables", "fiber");
$yummy = Array ("Pizza", "beer", "ice Cream");

$newphrase = Str_replace ($healthy, $yummy, $phrase);

Provides:2
$str = Str_replace ("ll", "" "," Good Golly Miss Molly! ", $count);
Echo $count;

Use the Str_replace function to specify the number of replacements

$array = Array (
Array (0,1,2)
);
function keywords ($str, $array)
{
$count = 0;
foreach ($array as $v) {
if (Strstr ($str, Strtolower ($v [0]))!==false) {
if ($count <=3) {
$tos = Strtolower ($v [0]);
$str =preg_replace ("/$tos/", "". $v [2]. ", $STR, 1);
$count + +;
Continue
}
}
}
return $str;
}

?>


http://www.bkjia.com/PHPjc/445401.html www.bkjia.com true http://www.bkjia.com/PHPjc/445401.html techarticle php str_replace function (can limit the number of replacements) This article is mainly about the Str_replace function to replace a single character, and replace the data, and specify the number of times the str_replace substitution, the last replacement ...

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