PHP uses an array to replace the occurrences of a string in a _php instance

Source: Internet
Author: User
Tags sprintf
Let's take a look at the SQL statement:

SELECT * FROM table where CTime >= ' [date-14] ' and CTime <= ' [date-1] ';

You want to change the date in parentheses of the above SQL into the following array of elements (' 2015-07-01 ', ' 2015-07-15 ');

Match with Regular: Find the first bracket part, replace it with the first one, then find the second one, and replace

Use the sprintf function: Because the date has been calculated, in order to replace it.

Because Markdown write regular is more troublesome, here is directly the film

PS: Imagine that there is only one time condition in SQL that needs to be replaced, it needs to be modified to

$sql = sprintf ($sql, $arr [0])

It would be nice if the sprintf function supports the second argument as an array. After checking it out, there is a solution:

Call_user_func_array () The official explanation is:

Call_user_func_array-invokes the callback function and takes an array parameter as the parameter of the callback function

Mixed Call_user_func_array (callable $callback, array $param _arr)

Call the first parameter as a callback function (callback) and pass the parameter array (Param_arr) as the parameter of the callback function.

That is: The first parameter is the name of the function you want to use (sprintf in the previous section), the second parameter is the parameter that will use the function, except that the argument is passed to Call_user_func_arrayok in the form of an array, so that a dynamic replacement is possible.

$param = $arr; Array_unshift ($param, $sql); $sql = Call_user_func_array (' sprintf ', $param);

Next introduce str_replace-substring substitution, array substitution

Description

Mixed Str_replace (mixed $search, mixed $replace, mixed $subject [, int & $count])

The function returns a string or an array. The string or array is the result of replacing all search in subject with replace.

If you have some special substitution requirements (such as regular expressions), you should use this function to replace ereg_replace () and Preg_replace ().

Parameters

If search and replace are arrays, then Str_replace () replaces the mappings for both subject. If the value of replace is less than the number of search, the extra substitution is done using an empty string. If search is an array and replace is a string, then the substitution of each element in search will always use that string. The conversion does not change case.
If both search and replace are arrays, their values will be processed sequentially.

Search

The target value of the lookup, which is needle. An array can specify multiple targets.

Replace

The replacement value for search. An array can be used to specify multiple substitutions.

Subject

Executes an array or string of replacements. That's haystack.

If subject is an array, the replace operation will traverse the entire subject, and the return value will be an array.

Count

Note: If specified, it will control the number of matches and replacements.

return value

The function returns the replaced array or string.

Release Notes

5.0.0 added the Count parameter.

4.3.3 function behavior changes. There is a bug--in the old version when search and replace two parameters are arrays, it causes the empty search index to be skipped, but does not move forward the replace internal pointer at the same time. This error occurs in PHP

4.3.3, any script that relies on this BUG should first remove the empty lookup value to simulate the original behavior.

4.0.5 most parameters can be arrays.

Example

Example #1 str_replace () Basic example

<?php//Assignment: $bodytag = Str_replace ("%body%", "Black", "");//assignment: Hll wrld f php$vowels = Array ("A", "E", "I", "O", "U", "A", "E", "I", "O", "U"), $onlyconsonants = Str_replace ($vowels, "", "Hello World of PHP");//assignment: Should eat pizza, be Er, 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);//assignment: 2$str = Str_replace ("ll", "", "Good Golly Miss Molly!", $count); Echo $count; >

Example #2 possible str_replace () Replacement Example

<?php//Replacement Order $str = "line 1\nline 2\rline 3\r\nline 4\n"; er = Array ("\ r \ n", "\ R"); $replace = '
';//first replace \ r \ n characters, so they will not be converted two times $newstr = Str_replace (er, $replace, $STR);//output F, because A is replaced by B, B is replaced by C, and so on ...//due to left to right Replace in turn, the end E is replaced by F $search = Array (' A ', ' B ', ' C ', ' D ', ' E '); $replace = Array (' B ', ' C ', ' D ', ' e ', ' f '); $subject = ' a '; Echo str _replace ($search, $replace, $subject);//output: Apearpearle pear//Due to the reasons mentioned above $letters = Array (' A ', ' P '); $fruit = Array (' Apple ', ' pear '); $text = ' A P '; $output = Str_replace ($letters, $fruit, $text); Echo $output;? >

Comments

Note: This function can be used safely with binary objects.

Caution

Understanding Replacement Order

Since the substitution of str_replace () occurs from left to right, a multiple substitution may replace the previously inserted value. See examples of this document.

Note:

The function is case-sensitive. Use Str_ireplace () to make case-insensitive substitutions.

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