John
John

Reputation: 61

Preg_replace in following string

I want to replace content from below string,

I want to replace line The part has been repaired to upto end p tag.

$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);

I tried above preg_replace function but its not working as expected.

Upvotes: 0

Views: 128

Answers (2)

Andreas
Andreas

Reputation: 23958

You can use strpos to find the position of your "end" and use substr to substring it.

$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

Upvotes: 0

Norris Oduro
Norris Oduro

Reputation: 1039

The preg_replace function returns the value of the string after the pattern has been applied to it. Assign a the result to a variable and print_r the variable.

$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);

Upvotes: 1

Related Questions