如何使用str_replace从同一字符串进行多次替换?

时间:2018-12-04 01:30:23

标签: php str-replace

我有此代码:

import numpy as np
import matplotlib.pyplot as plt

from lmfit.models import GaussianModel
from lmfit.lineshapes import gaussian

# create fake data that looks like yours
xdata = 7.670 + np.arange(41)*0.0010
ydata = gaussian(xdata, amplitude=-0.196, center=7.6881, sigma=0.001)
ydata += np.random.normal(size=41, scale=10.0)

# create gaussian model
gmodel = GaussianModel()

# fit data, giving initial values for amplitude, center, and sigma
result = gmodel.fit(ydata, x=xdata, amplitude=-0.1, center=7.688, sigma=0.005)

# show results
print(result.fit_report())
plt.plot(xdata, ydata, 'bo', label='data')
plt.plot(xdata, result.best_fit, 'r+-', label='fit')
plt.legend()
plt.show()

它可以工作,但是我觉得必须有一个速记,所以我尝试了这个:

[Model]]
    Model(gaussian)
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 21
    # data points      = 41
    # variables        = 3
    chi-square         = 5114.87632
    reduced chi-square = 134.602009
    Akaike info crit   = 203.879794
    Bayesian info crit = 209.020510
[[Variables]]
    sigma:      9.7713e-04 +/- 1.5456e-04 (15.82%) (init = 0.005)
    center:     7.68822727 +/- 1.5484e-04 (0.00%) (init = 7.688)
    amplitude: -0.19273945 +/- 0.02643400 (13.71%) (init = -0.1)
    fwhm:       0.00230096 +/- 3.6396e-04 (15.82%) == '2.3548200*sigma'
    height:    -78.6917624 +/- 10.7894236 (13.71%) == '0.3989423*amplitude/max(1.e-15, sigma)'
[[Correlations]] (unreported correlations are < 0.100)
    C(sigma, amplitude) = -0.577

但是它只会把事情搞砸了。我在做什么错了?

2 个答案:

答案 0 :(得分:0)

您可以使用PHP中的strtr函数来完成此操作。

$replace = [
  "href=\"index.php?" => "href=\"index.php?s=blog&",
  "src=\"images/" => "src=\"../../blog/images/"
];

$content = strtr($precontent, $replace);

答案 1 :(得分:0)

你可以这样尝试吗

$content=str_replace("src=\"images/", "src=\"../../blog/images/", str_replace("href=\"index.php?", "href=\"index.php?s=blog&", $precontent));