Solved

PHP preg_replace  string question

Posted on 2011-02-10
10
248 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
 
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
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 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

12 Experts available now in Live!

Get 1:1 Help Now