Conditional Matching

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Conditional Matching

Post by HiddenS3crets »

The goal of my preg_replace code is this: if a line begins with a bold, italic or underline tag opener (i.e. <b> <u> <i>) OR begins with NO tag, wrap the line in a <p> tag.

This means that lines that begin with <div.., <ul>, <li>, etc will not have a paragraph tag put around them.

How can I structure the regex to match either a opening b, u, or i tag, or no tag at all?

Examples

Code: Select all

This is a line of text with no tags around it.
 - regex will put <p> around this line
 
<b>this is some bold text</b>
 - regex will put <p> around this line too
 
<li>this is a list item</li>
 - regex will NOT put a <p> tag around this line
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Conditional Matching

Post by prometheuzz »

Try something like this (not properly tested!):

Code: Select all

$html ='<b>dsf</b>
 
text
 
<u>dfsdf
fds
fds</u>
 
<i>fgdfg
gfd
gf\
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Conditional Matching

Post by ridgerunner »

This one should do the trick:

Code: Select all

// regex: long commented version
$re_long = '/
^            # anchor regex to beginning of line
(            # capture whole line into group 1
[ \t]*+      # allow optional leading whitespace
  (?=        # make sure the line begins with...
    <[bui]>  # a <b>, <u> or <i>
  |          # or...
    [^<\s]   # a non-tag, non-whitespcase char
  ).++       # if so, match whole (non-empty) line
)            # end capture group 1
$            # anchor regex to end of line
/mx';
 
// regex: short uncommented version
$re_short = '/^([ \t]*+(?=<[bui]>|[^<\s]).++)$/m';
 
$text = preg_replace($re_short, '<p>$1</p>', $text);
Edited 2009-10-11 7:55am MDT: Added optional leading whitespace
Post Reply