Monday, 15 June 2015

php - preg_replace - array of individual replacements -


I am trying to get the following:

$ subject = 'ab A ';
$ search = 'A';
place of $ = '1';

Desired Results:

Array
(
[0] = & gt; 1 BA [1] => 1) )

Is there any way to get it with preg_replace?

preg_replace ('/ b'. $ Search. '(? = \ S + | $) / u', instead of $, array (subject $);

)

Cheers

I think this is not possible . You can specify the limit of replacement in the optional fourth parameter, but it always starts from the beginning.

It may be possible to achieve what you are looking for. All you have to do is split your strings on all the patterns of your search pattern, and after that you have to mess them together. If your search pattern is just a simple string, you can get it with explosion () If you need help locating this approach, then I would be happy to help.

Edit : See that this works for you:

  $ theme = 'now A'; $ Pattern = '/ a /'; $ Place = 1; // We split the string on all our matches and receive matches, even $ parts = preg_split ($ pattern, $ theme); Preg_match_all ($ patterns, $ theme, $ match); $ NumParts = count ($ parts); $ Result = array (); ($ I = 1; $ i  gt; & amp; $ value), if {if ($ index! = $ I & amp; $ index! = 0) $ Value = $ matches [0] [$ index - 1] $ Value; } // Bring it back together now $ result [] = implode ('', $ partsCopy); } Print_r ($ result);  

Note: this has not been tested yet. Please report that this works.

Edit 2 :

I tested it with your example, now decided some things and it works now (at least that Examples).


No comments:

Post a Comment