removing brackets and things in brackets

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sigarru
Forum Newbie
Posts: 1
Joined: Sat Mar 21, 2009 11:22 am

removing brackets and things in brackets

Post by sigarru »

Hi,

I am an average user in PHP however am not used in string processing etc... and am facing this problem... I'm having data like:

ABCDEF [abc]
ADDDD [xxy3]

and i want it to be ABCDEF and ADDDD after i process it.. In other words i want to remove all things that are in square brackets including the square brackets (in any way or length)...

Can anyone help me?

Thanks
Sigarru
JasonDFR
Forum Commoner
Posts: 40
Joined: Wed Jan 07, 2009 3:51 am

Re: removing brackets and things in brackets

Post by JasonDFR »

Let me know how this works:

Code: Select all

<?php
$data = 'ABCDEF [dfsgfd]';
$data = trim($data);
$pattern = '/\[\w*?\]/';
$clean = trim(preg_replace($pattern, '', $data));
 
echo 'Before: ' . $data . '<p />';
echo 'After: ' . $clean . '<p />';
 
exit;
Post Reply