Results 1 to 3 of 3

Thread: [Resolved] Preg_Match Pattern

  1. #1

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Resolved [Resolved] Preg_Match Pattern

    Hey I have a patteren which will match the [] (bbCode) style tags in a piece of text, but i cannot get it to match the tags if the span over mulitple lines

    PHP Code:
    $string = <<<eof
    sd as da [b][u][i][c]sample text[/c][/i][/u][/b] asd

    fasdf
    [c][small][b]Hello World[/b][/small][/c]

    adhsdaof usdsdiof ;adsljfdf as [u]wow[/u]sdasd
    sdfgsdfs[r]way
     out[/r]
    eof;

    $pattern '/\[.*?\].*?\[\/.*?\]/U';
    $match_count preg_match_all($pattern,$string,$array);
    print_r($array); 
    output
    Code:
    Array
    (
        [0] => Array
            (
                [0] => [b ][u ][i ][c ]sample text[/c ][/i ][/u ][/b ]
                [1] => [c ][small ][b ]Hello World[/b ][/small ][/c ]
                [2] => [u ]wow[/u ]
            )
    
    )

    If anyone can help me match code over multiple lines would be great.

    Thanks
    Last edited by john tindell; Feb 6th, 2006 at 04:55 PM.

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Preg_Match Pattern

    Muli-lines are bad for pattern matching, you can match a new line with the following:

    PHP Code:
      $pattern '#\[(.*?)\]((\n){0,})(.*?)((\n){0,})\[\/(.*?)\]#si';
      
    $match_count preg_match_all($pattern,$string,$array);
      
    print_r($array); 
    Should do it

    Cheers,

    Ryan Jones
    My Blog.

    Ryan Jones.

  3. #3

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Preg_Match Pattern

    Hey thanks for the reply, I kept having problems with matching the tags that i want so i ended up using the tag names to create the pattern.

    PHP Code:
    $tags = array(
        
    'b' => 'font-weight:bold;',
        
    'u' => 'text-decoration: underline;',
        
    'i' => 'font-style: italic;',
        
    'small' => 'font-size:10px;',
        
    'large' => 'font-size:16px;',
        
    'hb' => 'font-weight:bold;font-size:16px;',
        
    'hu' => 'text-decoration: underline;font-size:16px;',
        
    'hbu' => 'font-weight:bold;text-decoration: underline;font-size:16px;',
        
    'h' => 'font-size:16px;',
        
    'c' => 'text-align:center;',
        
    'l' => 'text-aligb:left;',
        
    'r' => 'text-align:right;');
            
            
    foreach(
    $tags as $tag =>$val)
    {
        
    $list .= $tag "|";
    }
    $list substr($list,0,strlen($list)-1);            
    $pattern '#\[('$list .')\].*?\[\/('$list .')\]#si';
    $match_count preg_match_all($pattern,$string,$array); 
    It matched and works, though im not 100% sure why it works over multiple lines when i havent asked it too? O well. Thanks for the reply.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width