Remove parentheses and its contents in PHP


 Remove parentheses and its contents in PHP

Come up with a simple tutorial that will save time for sure. Let's just remove parentheses and its contents in a string, of course, in PHP. For the record, we have nothing against them, but sometimes it is useful to clean and sort content in a more rational action and therefore seoĆ­stica.
To remove the parentheses and its contents need only use a regular expression to find and replace the parentheses around an empty, that is. " For this we use the PHP function preg_replace.

Code to remove parentheses

This would be the resulting code:
preg_replace ( "/\((.*?)\)/ i", "", $ cadenaOriginal);
( "/\((.*?)\)/i" , "" , $ CadenaOriginal );
The first value is the regular expression that selects all instances of what we want, in this case a parenthesis and whatever you have inside; the second contains the original value chain we want to remove the parentheses.
For example, if you  chain: "There is no place like home (except outside houses with free wifi ) to plant a pine)"
The result: "There is no better place than home to plant a pine)"
Simple, right?

Bonus track, remove brackets, quotation marks and other signs

To remove brackets, quotation marks (both single and double) and other undesirable characters for some reason all we have to do is modify the regular expression, these would be the most common examples:

Remove brackets and its contents

preg_replace ( "/\[(.*?)\]/ i", "", $ cadenaOriginal);( "/\[(.*?)\]/i" ,  "" , $ CadenaOriginal );

Remove the quoted text

preg_replace ( "/\"(.*?)\"/ i", "", $ cadenaOriginal);( "/\"(.*?)\"/i" ,  "" , $ CadenaOriginal );
The use you want to give it's up to you, for instance, a few weeks ago I decided to restructure a web of phrases that I have for years and was abandoned. The structure of sentences containing the author in brackets, to improve the indexing of the web site prepared a script that took the author of each of the sentences using regular expressions like this and turned his name into a custom taxonomy of WordPress , so in the end was much better organized content. It is an example of use.
And you could do with whatever we wanted. 
You're welcome.

Final Words

If You Need Any Kind Of Information about Blogger Contact us