PHP uses arrays to replace matching items in strings in sequence, and php array to replace string _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags array to string
PHP uses arrays to replace matching items in strings in sequence, and php arrays to replace strings. PHP uses an array to replace the matching items in the string in sequence. The php array replaces the string to see the SQL statement: select * fromtablewherectime [date-14] andctime [date-1]; to replace the matching items in the above PHP strings with arrays in sequence, replace the php array with strings.

First, let's look at an SQL statement:

select * from table where ctime >= '[date-14]' and ctime <= '[date-1]';

You want to replace the dates represented by braces in the preceding SQL statement with the element array ('2017-07-01 ', '2017-07-15') in the following array ');

Regular Expression Matching: find the first bracket, replace it with the first element, find the second element, and then replace it.

Use the sprintf function: because the date has been calculated, it can be replaced in order.

Because markdown is difficult to write regular expressions, we can directly split it here.

Ps: imagine that there is only one time condition to replace in SQL, and it needs to be modified

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

To put it bluntly, it would be nice if the sprintf function supports the second parameter as an array. After checking the information, you can find a solution:

The official explanation of call_user_func_array () is:

Call_user_func_array-call the callback function and use an array parameter as the callback function parameter.

mixed call_user_func_array ( callable $callback , array $param_arr )

Call the first parameter as the callback function, and pass in the parameter array as the parameter (param_arr) of the callback function.

That is to say, the first parameter is the name of the function you want to use (sprintf in the above section), the second parameter is the parameter of the function to be used, but the parameter is passed to call_user_func_arrayok in an array, in this way, dynamic replacement can be implemented.

$param = $arr;array_unshift($param,$sql);$sql = call_user_func_array('sprintf',$param);

Next we will introduce str_replace-replace the substring and replace the array.

Description

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

This function returns a string or array. This string or array is the result after all the searches in the subject are replaced by replace.

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

Parameters

If search and replace are arrays, str_replace () will replace the ing between the two in subject. If the number of replace values is less than the number of search values, null strings are used for redundant replace. If search is an array and replace is a string, the replacement of each element in search always uses this string. This conversion will not change the case sensitivity.
If both search and replace are arrays, their values are processed in sequence.

Search

The target value, that is, needle. Multiple targets can be specified in an array.

Replace

The replacement value of search. An array can be used to specify multiple replicas.

Subject

Execute the replaced array or string. That is, haystack.

If the subject is an array, the replacement operation traverses the entire subject, and the return value is also an array.

Count

Note: If specified, it controls the number of matching and replacement times.

Return value

This function returns the replaced array or string.

Version description

5.0.0 adds the count parameter.

4.3.3 function behavior changes. In the old version, there is a BUG. when both search and replace parameters are arrays, empty search indexes will be skipped, but the replace internal pointer is not moved forward at the same time. This error occurs in PHP

4.3.3 any script dependent on this BUG should first remove the null search value to simulate the original behavior.

4.0.5 most parameters can be an array.

Example

Example #1 basic Example of str_replace ()

<? Php // value assignment:$ Bodytag = str_replace ("% body %", "black ",""); // Value assignment: Hll Wrld f PHP $ vowels = array (" a "," e "," I "," o "," u "," ", "E", "I", "O", "U"); $ onlyconsonants = str_replace ($ vowels, "", "Hello World of PHP"); // value assignment: you shoshould eat pizza, beer, and ice cream every day $ phrase = "You shoshould eat fruits, vegetables, and fiber every day. "; $ healthy = array (" fruits "," vegetables "," fiber "); $ yummy = array (" pizza "," beer "," ice cream "); $ newphrase = str_replace ( $ Healthy, $ yummy, $ phrase); // value: 2 $ str = str_replace ("ll", "", "good golly miss molly! ", $ Count); echo $ count;?>

Example #2 possible str_replace () replacement examples

<? Php // replacement sequence $ str = "Line 1 \ nLine 2 \ rLine 3 \ r \ nLine 4 \ n"; $ order = array ("\ r \ n ", "\ n", "\ r"); $ replace ='
'; // Replace \ r \ n characters first, so they are not converted twice $ newstr = str_replace ($ order, $ replace, $ str); // output F, because A is replaced by B, B is replaced by C, and so on... // because E is replaced from left to right, F is replaced by $ 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 // for the reason mentioned above $ letters = array ('A', 'P '); $ fruit = array ('apple', 'pear '); $ text = 'A P'; $ output = s Tr_replace ($ letters, $ fruit, $ text); echo $ output;?>

Note

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

Caution

Understanding the order of replacement

Because the replacement of str_replace () is performed from left to right, the previously inserted values may be replaced when multiple replacement is performed. See the example in this document.

Note:

This function is case sensitive. Use str_ireplace () for case-insensitive replacement.

Articles you may be interested in:
  • Php separates strings into string arrays by uppercase letters
  • Php code that concatenates a one-dimensional or multi-dimensional array into a string
  • PHP converts a string separated by commas, spaces, and carriage return to an array function
  • PHP array and string conversion implementation method
  • Summary based on common php functions (array, string, time, file operations)
  • Php array and string conversion functions
  • Convert the array into a string and save it to the function code in the database.
  • Use the explode function in php to split strings into arrays
  • Example of converting a two-dimensional php array into a string
  • Php method to determine whether a string exists in an array element
  • Php randomly splits strings into arrays of different lengths
  • PHP implements conversion from multi-dimensional array to string and multi-dimensional array to one-dimensional array

First, let's look at an SQL statement: select * from table where ctime = '[date-14]' and ctime = '[date-1]'; to put the above...

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.