Solved

PHP preg_replace  string question

Posted on 2011-02-10
10
250 Views
Last Modified: 2012-05-11
The following does not seem to work, can you help correct it?

I want to delete "comments"  beginning with    //       UP TO the next <br> OR end of string

preg_replace('@(?<!:)//.*((?=<br( /)*>)|$)@U',  '',  $OrigStringWithComments);

ex:  "ABC //comment1<br>DEF//comment2"

thanks!
0
Comment
Question by:willsherwood
  • 5
  • 5
10 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34866028
I changed the criteria for the br tag slightly - try this:
preg_replace('@(?<!:)//.*((?=<br([ /])*>)|$)@U',  '',  $OrigStringWithComments);
0
 

Author Comment

by:willsherwood
ID: 34866061
Super thanks - i'm on it, stay tuned momentarily
0
 

Author Comment

by:willsherwood
ID: 34866092
sorry still shows  //comments
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 35

Expert Comment

by:Terry Woods
ID: 34866094
In fact, the round brackets aren't needed around the bit I changed:
preg_replace('@(?<!:)//.*((?=<br[ /]*>)|$)@U',  '',  $OrigStringWithComments);
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34866105
Can you post the code you tested with? The example you provided seems to work with my code, and actually even works with your code (but my change was still necessary, I believe)
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34866109
I mean provide the text that you tested against....
0
 

Author Comment

by:willsherwood
ID: 34866439
Sorry for not having narrowed it down further.
(I had of course abstracted in my original example)
The problem centers around additional characters "in" the comment (prior to <br>  or  <br /> )

here is a whittled down test that when   \n is removed, it works.

"  Georgia Island   //input\n<br>Williams ";

is there a way to ignore  \n and other extraneous chars?

thanks for your patience!
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 34866478
I think you need the multiline pattern modifier (m) enabled:

preg_replace('@(?<!:)//.*((?=<br[ /]*>)|$)@Um',  '',  $OrigStringWithComments);
0
 

Author Comment

by:willsherwood
ID: 34866711
YAY
perfect.
THANK YOU!
0
 

Author Closing Comment

by:willsherwood
ID: 34866714
super!
0

Featured Post

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.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to dynamically set the form action using jQuery.

832 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