Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Remove specific elements from a string

Posted on 2011-09-29
7
Medium Priority
?
307 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 111

Expert Comment

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

Accepted Solution

by:
Ray Paseur earned 800 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 1200 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
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 
LVL 8

Assisted Solution

by:rationalboss
rationalboss earned 1200 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

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 dynamically set the form action using jQuery.
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.

722 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