• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

PHP preg_replace string question

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
willsherwood
Asked:
willsherwood
  • 5
  • 5
1 Solution
 
Terry WoodsIT GuruCommented:
I changed the criteria for the br tag slightly - try this:
preg_replace('@(?<!:)//.*((?=<br([ /])*>)|$)@U',  '',  $OrigStringWithComments);
0
 
willsherwoodAuthor Commented:
Super thanks - i'm on it, stay tuned momentarily
0
 
willsherwoodAuthor Commented:
sorry still shows  //comments
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Terry WoodsIT GuruCommented:
In fact, the round brackets aren't needed around the bit I changed:
preg_replace('@(?<!:)//.*((?=<br[ /]*>)|$)@U',  '',  $OrigStringWithComments);
0
 
Terry WoodsIT GuruCommented:
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
 
Terry WoodsIT GuruCommented:
I mean provide the text that you tested against....
0
 
willsherwoodAuthor Commented:
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
 
Terry WoodsIT GuruCommented:
I think you need the multiline pattern modifier (m) enabled:

preg_replace('@(?<!:)//.*((?=<br[ /]*>)|$)@Um',  '',  $OrigStringWithComments);
0
 
willsherwoodAuthor Commented:
YAY
perfect.
THANK YOU!
0
 
willsherwoodAuthor Commented:
super!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now