Solved

PHP preg_replace  string question

Posted on 2011-02-10
10
253 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
[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
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to count occurrences of each item in an array.

752 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