Remove All the occurrence of Word from Sentence Using Regular Expression

Viewed 32

Hello I want to remove words from sentence below. how can i do this with regular expressions

ABC (+£768.00):Yes, 1 Top Access with 2 Socket (+£28.00):Yes , 2 USB and 2 RJ45 (Black)

I want to remove all the occurrence of words which start with "(+" and ends with "):".

For ex: i want to remove "(+£768.00):" and "(+£28.00):" from above line.

Please help me with this.

Thank You

1 Answers

Just do as the pattern demands using below regex:

\(\+.*?\)\:

Match (+ as is by escaping followed by non-greedy matches of 0 or more characters ending with ): again needing to be escaped. Use preg_replace to replace the match with an empty string.

<?php

echo preg_replace('/\(\+.*?\)\:/', '', 'ABC (+£768.00):Yes, 1 Top Access with 2 Socket (+£28.00):Yes , 2 USB and 2 RJ45 (Black)');

Online Demo

Related