Solved

Remove specific elements from a string

Posted on 2011-09-29
7
300 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 108

Expert Comment

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

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Read about why website design really matters in today's demanding market.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now