Solved

Remove specific elements from a string

Posted on 2011-09-29
7
306 Views
Last Modified: 2012-05-12
Hi Experts!

How to remove elements in square brackets with square brackets from a string?
Example, before:
[Software][NK]
something else, another element, etc.

[Lorem][ipsum]
test, etc. etc

Open in new window

after:
something else, another element, etc.

test, etc. etc

Open in new window

With fixed elements in brackets it would be easy, just preg_replace, but what if there may be anything in brackets like in example above?

Thanks for any help.
0
Comment
Question by:Zado
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36813922
Still uses preg_replace.  You have to escape the brackets.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 200 total points
ID: 36813948
You might want to make a separate test for empty lines, but this regex will eliminate the bracketed substrings.
<?php // RAY_temp_zado_brackets.php
error_reporting(E_ALL);
echo "<pre>";


// THE TEST DATA
$str = <<<EOS
[Software][NK]
something else, another element, etc.

[Lorem][ipsum]
test, etc. etc
EOS;

// THE REGEX TO REMOVE THE BRACKETED SUBSTRINGS
$rgx
= '#'       // REGEX DELIMITER
. '\[.*?\]' // BRACKETS WRAPPING ANYTHING
. '#'       // REGEX DELIMITER
;

var_dump($str);

$new = preg_replace($rgx, NULL, $str);
var_dump($new);

Open in new window

0
 
LVL 8

Assisted Solution

by:rationalboss
rationalboss earned 300 total points
ID: 36813975
$data="
[some]
text here

[hello]
world hello!

";

$data = preg_replace("/(\[.+?)+(\])\s{1,2}/i","",$data);

echo nl2br($data);

Open in new window

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 8

Assisted Solution

by:rationalboss
rationalboss earned 300 total points
ID: 36813989
$data = preg_replace("/(\[.+?)+(\])\s{1,2}/i","",$data); // NO NEED for the /i though. you can drop that and make it simply:

$data = preg_replace("/(\[.+?)+(\])\s{1,2}/i","",$data);

Open in new window


this means that everything inside the square brackets [ and ] will be stripped.
The \s{1,2} is to also remove the line from the text. without those, the text you strip will have blank lines for lines with square brackets.

you may wish to drop that \s{1,2} if you want to remove texts in square brackets with other text like:
there are some [other] text here.

Open in new window

0
 
LVL 8

Author Comment

by:Zado
ID: 36814019
Excellent! Thanks a lot :-)
Btw, I know how to use functions, but I don't know what this piece means: '/(\[.+?)+(\])\s{1,2}/i', is there any web page I can learn this?
0
 
LVL 8

Author Comment

by:Zado
ID: 36814060
Thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
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.

631 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