Online, urgent. PHP replaces the string at the specified position

Source: Internet
Author: User
Online, urgent. PHP replaces the string $ a = '36, 0.00, 0.00, 100 | 37, 0.00, 0.00, 100 | 38, 0.00, 0.00, 100 | 39, 0.00, 0.00, 100 ';
$ B = '38 ';
$ C = '85 ';

How to replace it with PHP...

That is to say, when $ a contains the words in $ B. Replace 0.00 in 38, 0.00, 100, and 100 with a number in $ c.

If $ B = '37'

Replace 0.00 in 37, 0.00, 100, and 100 with a number in $ c.


Thank you!


Reply to discussion (solution)

Through php5.3 +

$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85';echo preg_replace_callback("/(^|\|$b,0.00,0.00,)100/", function($m) use ($c){ return $m[1].$c;}, $a);
36, 0.00, 0.00, 100 | 37, 0.00, 0.00, 100 | 38, 0.00, 0.00, 85 | 39, 0.00, 0.00, 100

Php5.5-writeable

echo preg_replace("/(^|\|$b,0.00,0.00,)100/e", "'$1'.'$c'", $a);

Replace the string specified in the original string with a new substring
$ Str2 = "XX ";
$ Str1 = "**";
$ Str = "XX Company is a high-tech enterprise with computer software technology as its core, over the years, Alibaba Cloud has been committed to industry management software development, digital publication production, integrated application of computer network systems, and industry e-commerce website development, industries involving production, management, control, warehousing, logistics, marketing, and service ";
Echo str_ireplace ($ str2, $ str1, $ str );
?>

Through php5.3 +

$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85';echo preg_replace_callback("/(^|\|$b,0.00,0.00,)100/", function($m) use ($c){ return $m[1].$c;}, $a);
36, 0.00, 0.00, 100 | 37, 0.00, 0.00, 100 | 38, 0.00, 0.00, 85 | 39, 0.00, 0.00, 100



Boss. If PHP5.0 is compatible, how can I write it.

#2 no?

#2 no?



Helping me out
$ A = '36, 0.00, 0.00, 100 | 37, 0.00, 0.00, 100 | 38, 0.00, 0.00, 100 | 39, 0.00, 0.00, 100 ';

The value of the red part is not fixed .. So how can we get this formal ....... Thank you.

Not fixed?
Isn't it fixed?

Not fixed?
Isn't it fixed?


Isn't it fixed? if it's fixed, is it useful?

$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.01,0.00,100|39,0.00,0.00,100';$b='38';$c='85';echo "{$a}
";echo preg_replace_callback("/(^|\|$b,0.00,0.00,)100/", function($m) use ($c){ return $m[1].$c;}, $a).'
';echo preg_replace_callback("/(^|\|$b,\d\.\d{1,2},\d\.\d{1,2},)100/", function($m) use ($c){ return $m[1].$c;}, $a);


\ D \. \ d {1, 2} is the 0.00 in the middle of the confirmation, and other formats are adjusted by yourself

Okay ,.,. I am wrong .... I have no answer to the table. I thought it was very simple. As a result, I tried it and found that it was not enough.

The actual result is as follows:

$ A = '36, 0.00, 0.00, 100 | 37, 0.00, 0.00, 100 | 38, 0.00, 0.00, 100 | 39, 0.00, 0.00, 100 ';
$ B = '38 ';
$ C = '85 ';

That is to say, when $ a contains the words in $ B. Take 0.00 from 38, 0.00, 100, and 100, and then subtract the number in $ c from this 100.
The final value is 36, 0.00, 0.00, 100 | 37, 0.00, 0.00, 100 | 38, 0.00, 0.00, 15 | 39, 0.00, 0.00, 100

Then import the database ,....... Others I want this... Simply think about the problem

Please help me.

$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace_callback("/(^|\|$b,0.00,0.00,)(100)/", function($m) use ($c){ return $m[1].($m[2]-$c);}, $a);
36, 0.00, 0.00, 100 | 37, 0.00, 0.00, 100 | 38, 0.00, 0.00, 15 | 39, 0.00, 0.00, 100

Oh, your php version is not that high
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace("/(^|\|$b,0.00,0.00,)(100)/e", "'$1'.($2-$c)", $a);
36, 0.00, 0.00, 100 | 37, 0.00, 0.00, 100 | 38, 0.00, 0.00, 15 | 39, 0.00, 0.00, 100

If all are variables
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace("/((?:^|\|$b),(?:[\d.]+,){2})(\d+)/e", "'$1'.($2-$c)", $a);
36, 0.00, 0.00, 100 | 37, 0.00, 0.00, 100 | 38, 0.00, 0.00, 15 | 39, 0.00, 0.00, 100

$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace_callback("/(^|\|$b,0.00,0.00,)(100)/", function($m) use ($c){ return $m[1].($m[2]-$c);}, $a);
36, 0.00, 0.00, 100 | 37, 0.00, 0.00, 100 | 38, 0.00, 0.00, 15 | 39, 0.00, 0.00, 100

Oh, your php version is not that high
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace("/(^|\|$b,0.00,0.00,)(100)/e", "'$1'.($2-$c)", $a);
36, 0.00, 0.00, 100 | 37, 0.00, 0.00, 100 | 38, 0.00, 0.00, 15 | 39, 0.00, 0.00, 100

If all are variables
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace("/((?:^|\|$b),(?:[\d.]+,){2})(\d+)/e", "'$1'.($2-$c)", $a);
36, 0.00, 0.00, 100 | 37, 0.00, 0.00, 100 | 38, 0.00, 0.00, 15 | 39, 0.00, 0.00, 100



No more. Tears wow... Thank you very much... @ Xuzuning
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.