Asked  7 Months ago    Answers:  5   Viewed   49 times

I'm just wondering how I could remove the text between a set of parentheses and the parentheses themselves in php.

Example :

ABC (Test1)

I would like it to delete (Test1) and only leave ABC



$string = "ABC (Test1)";
echo preg_replace("/([^)]+)/","",$string); // 'ABC '

preg_replace is a perl-based regular expression replace routine. What this script does is matches all occurrences of a opening parenthesis, followed by any number of characters not a closing parenthesis, and again followed by a closing parenthesis, and then deletes them:

Regular expression breakdown:

/  - opening delimiter (necessary for regular expressions, can be any character that doesn't appear in the regular expression
( - Match an opening parenthesis
[^)]+ - Match 1 or more character that is not a closing parenthesis
) - Match a closing parenthesis
/  - Closing delimiter
Wednesday, March 31, 2021
answered 7 Months ago

Without seeing your code I cannot be sure, but my guess is you are using single quotes ('n') instead of double quotes ("n").

PHP will only evaluate escape sequences if the string is enclosed in double quotes. If you use 'n', PHP will just take that as a literal string. If you use "n", PHP will parse the string for variables and escape sequences and print a new line like you are expecting.

Wednesday, March 31, 2021
answered 7 Months ago

Try using negative margins. It may take a bit of playing with the numbers to get it right, but I've done it before and it worked out well.

Wednesday, July 28, 2021
answered 3 Months ago

You may check if a string contains [, ], (<no_parentheses_here>) or [no_brackets_here] substrings and remove them while there is a match.

import re                                    # Use standard re
s='{[a] abc (b(c)d)}'
rx = re.compile(r'([^()]*)|[[^][]*]|[{}]')
while                          # While regex matches the string
    s = rx.sub('', s)                        # Remove the matches
print(s.strip())                             # Strip whitespace and show the result
# => abc

See the Python demo

It will also work with paired nested (...) and [...], too.

Pattern details

  • ([^()]*) - (, then any 0+ chars other than ( and ), and then )
  • | - or
  • [[^][]*] - [, then any 0+ chars other than [ and ], and then ]
  • | - or
  • [{}] - a character class matching { or }.
Friday, August 20, 2021
answered 2 Months ago

The solution is probably for you to strip slashes.

The slashes are automatically added when data comes from POST or GET. This is known as magic quotes and by default are enabled.

You can remove these slashes by using stripslashes()


$text = $_POST['txtarea']; // from textarea
  $text = stripslashes($text);
  // strip off the slashes if they are magically added.
$text = htmlentities($text);
// what htmlentities here does is really to convert:
//   & to &amp;
//   " to &#039;
//  and change all < and > to &lt; and &gt; respectively. this will automatically disable html codes in the text.
echo '<pre>'.$text.'</pre>';



Wednesday, August 25, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :