Solved

Remove specific elements from a string

Posted on 2011-09-29
7
303 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 109

Expert Comment

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

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use embargo Date/time in php 29 39
echo paypal data on screen 5 53
tutorial for ebay api 3 35
PHP PDO   How do I correctly remove record using DELETE FROM? 1 21
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
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
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

810 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