php substr Chinese garbled solution

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags array data function network network programming php php tutorial programming

This article is mainly introduced str_replace function to replace a single character, and replace the data, and specify the number of str_replace replacement, the last replacement is very useful, especially in the SEO optimization methods.
/ mixed str_replace (mixed $ search, mixed $ replace, mixed $ subject [, int & $ count])

A function returns a string or replacement value for the array-replacement value given in all searches for the problem.

If you do not like rules that need to be replaced (like regular expressions), you should always use this function instead of the ereg_replace () or preg_replace () function.
* /
// provides: <body text = 'black'>
$ bodytag = str_replace ("% body%", "black", "<body text = '% body%'>");

// 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 str_replace function to specify the number of substitutions

$ 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 /", "<a href=".$v[1]." target=_blank>". $ v [2]. "</a>", $ str, 1) ;
$ count ++;
continue
}
}
}
return $ str;
}

?>

Related Article

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.