Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to remove the content between some square brackets

Posted on 2007-10-10
10
Medium Priority
?
510 Views
Last Modified: 2013-12-13
Hey experts!

Let's say I have a string in a variable called $string, is it possible to remove the content between some square brackets if there is some in the string?

Thanks a lot for the help,
Frank
0
Comment
Question by:The_Kingpin08
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 8

Expert Comment

by:Bradley Haynes
ID: 20053201
Yes by using RegExp as in PERL which is the best language for text extraction and reporting.
0
 
LVL 8

Expert Comment

by:Bradley Haynes
ID: 20053206
Here is a link to PHP regular expression examples.

http://www.roscripts.com/PHP_regular_expressions_examples-136.html
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20053216
Yep regular expression is the way to go. If you post exactly what you want to remove we may be able to provide you with a specific example :)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:The_Kingpin08
ID: 20053317
I just start reading but I'm wondering; will this work even if I don't know what's the content inside the square brackets? I mean, it's content will probably be dynamic, so the value will change. I need to take it out along with the brackets themselve without knowing what's inside..

Will this be possible?
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20053334
It shouold be very possible with regular expressions! Let me know if you need further help.
0
 

Author Comment

by:The_Kingpin08
ID: 20053405
I'm not sure why this don't work:
$result = preg_replace ("/[(.*)]/", "", $string);
0
 

Author Comment

by:The_Kingpin08
ID: 20053624
Anyone can help me out please =)

Thanks
0
 

Author Comment

by:The_Kingpin08
ID: 20053693
$result = preg_replace ("/[][]/", "", $string);
this strips the brackets, but not the content... Anyone can help please.
0
 
LVL 5

Accepted Solution

by:
siva_siva earned 2000 total points
ID: 20055489
Hi use this code it will remove the square bracket string,

<?php
$string = "Test [square] bracket removal";
echo "$string<br>";
echo preg_replace('/\[[^]]+]/', '', $string)
?>
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The viewer will learn how to count occurrences of each item in an array.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question