0

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.

5
  • So you want to replace everything to the end of the string with a </p>. so your string becomes <p>701082 Range Control Board from Dacor is a manufacturer approved part.</p> Commented Sep 12, 2017 at 6:32
  • 1
    Why regex? 3v4l.org/tR5tt Commented Sep 12, 2017 at 6:35
  • @NorrisOduro Yes, Commented Sep 12, 2017 at 6:36
  • check my answer Commented Sep 12, 2017 at 6:37
  • make sure you consider @Andreas answer If you need something other than regex. And dont forget to mark my answer if it is helpful Commented Sep 12, 2017 at 6:43

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.