Preg_replace 在以下字符串中

Preg_replace in following string

我想替换下面字符串中的内容,

我想替换第 The part has been repaired to 行直到结束 p 标记。

$text = "<p>701082 Range Control Board from Dacor is a manufacturer approved part. The part has been repaired to Dacor's specifications resulting in the highest performance with superior quality</p>";

preg_replace('/The part has been repaired to.*?<\/p>/U', '</p>', $text);

print_r($text);

我尝试了上面的 preg_replace 功能,但它没有按预期工作。

preg_replace 函数 returns 应用模式后字符串的值。将结果分配给一个变量,print_r 变量。

$text = "<p>701082 Range Control Board from Dacor is a manufacturer approved part. The part has been repaired to Dacor's specifications resulting in the highest performance with superior quality</p>";

$result  = preg_replace('/The part has been repaired to.*?<\/p>/U', '</p>', $text);

print_r($result);

您可以使用 strpos 找到您的 "end" 的位置并使用 substr 对其进行子字符串化。

$text = "<p>701082 Range Control Board from Dacor is a manufacturer approved part. The part has been repaired to Dacor's specifications resulting in the highest performance with superior quality</p>";

Echo substr($text, 0, strpos($text, "The part has been repaired to")). "</p>";

https://3v4l.org/tR5tt