Solved

Remove specific elements from a string

Posted on 2011-09-29
7
305 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
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!

 
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

Expert Comment

by:rationalboss
ID: 36814044
0
 
LVL 8

Author Comment

by:Zado
ID: 36814060
Thanks!
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

710 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