Solved

PHP preg_replace  string question

Posted on 2011-02-10
10
251 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 dynamically set the form action using jQuery.

809 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