, PHP replaces the string at the specified location

Source: Internet
Author: User
Online, etc., urgent. PHP replaces the string at the specified location

$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 do a replacement with PHP ...

This means that when $ A contains the number of words in $b. Replace 100 in 38,0.00,0.00,100 with the number in the $c.

If $b= ' 37 '

Then replace the 100 in 37,0.00,0.00,100 with the number in the $c.


Thank you, everyone.
------to solve the idea----------------------
php5.3+ through
$a = ' 36,0.00,0.00,100
------to solve the idea----------------------
37,0.00,0.00,100
------to solve the idea----------------------
38,0.00,0.00,100
------to solve the idea----------------------
39,0.00,0.00,100 ';
$b = ' 38 ';
$c = ' 85 ';

echo Preg_replace_callback ("/(^
------to solve the idea----------------------
\
------to solve the idea----------------------
$b, 0.00,0.00,) 100/", function ($m) use ($c) {return $m [1]. $c;}, $a);
36,0.00,0.00,100
------to solve the idea----------------------
37,0.00,0.00,100
------to solve the idea----------------------
38,0.00,0.00,85
------to solve the idea----------------------
39,0.00,0.00,100
  • 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.