Technical stickers about PHP string replacement aspects of the frequently encountered see description

Source: Internet
Author: User
Instance Code
$string = ' key An array group can contain both integer and string key names, 12345678 because PHP does not actually differentiate between indexed arrays and associative arrays. If no key name is specified for the given value, the current largest integer index value is taken, and the new key name is the value plus one. If the specified key name already has a value, the value is overwritten. '; $keyArray =array (' array ', ' Integer ', ' 2345 ', ' Key Name '); $replacement =array (' 0 ' =>array (' 0 ' + ') ' key ', ' 1 ' = ' = ' Www.baidu.com ", ' 2 ' = ' 2 '), ' 1 ' =>array (' 0 ' = ' = ' array can ', ' 1 ' = ' http://www.baidu.com ', ' 2 ' = ' 2 '), ' 2 ' = = Array (' 0 ' = ' + ' arrays ', ' 1 ' = ' = ' http://www.baidu.com ', ' 2 ' = ' 1 ')); can achieve the control of the number of replacements, not only to replace only once, for example, $limit is 2 when a word appears a lot of time to eat only replace 2 times,//-1 means to replace all. Both $search and $replace can be strings or arrays, but must correspond to function Str_replace_limit ($search, $replace, $content, $limit =-1) {if (Is_array ($ Search) {foreach ($search as $k = + $v) {$search [$k]= '. Preg_quote ($search [$k], '). '        `'; }}else{$search = "". Preg_quote ($search, "). '    `';     } $content =preg_replace ("/alt= ([^ >]+)/is", ", $content); Return Preg_replace ($search, $replace, $content, $limit); }function keylinks ($string, $replacement) {for ($i =0; $i
   
  
Desired features

The second time has replaced the array but replaced it with the third time.

Want to achieve is the second time to replace the time there is no way to skip the already replaced


Reply to discussion (solution)

foreach ($replacement as $v) {  $p = '/([^>]) ('. Preg_quote ($v [0]). ') ([^<])/s ';  $r = "$1$2$3";  $n = $v [2];  $string = Preg_replace ($p, $r, $string, $n);} Echo $string;
The key an array group can contain both integer and string key names, 12345678 because PHP does not actually differentiate between indexed arrays and associative arrays.
If no key name is specified for the given value, the current largest integer index value is taken, and the new key name is the value plus one. If the specified key name already has a value, the value is overwritten.

foreach ($replacement as $v) {  $p = '/([^>]) ('. Preg_quote ($v [0]). ') ([^<])/s ';  $r = "$1$2$3";  $n = $v [2];  $string = Preg_replace ($p, $r, $string, $n);} Echo $string;
The key an array group can contain both integer and string key names, 12345678 because PHP does not actually differentiate between indexed arrays and associative arrays.
If no key name is specified for the given value, the current largest integer index value is taken, and the new key name is the value plus one. If the specified key name already has a value, the value is overwritten.



Tried this method, but it didn't work. I don't know if there's any other way to do it.
  • 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.