Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2004
    Location
    UK
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    BBcode tags with optional parameters!

    Does anyone know how would you define a BBcode tag with optional parameters? E.g. using preg_replace having [image align=right width=200] but making it so one or more parameters can be ignored or the order of them can be changed without writing loads of combinations of preg_replace code?

    At the moment I have some regex like:

    '#\[image align=(.*?) width=(.*?) height=(.*?)\]#msi'

    but obviously the problem with that is all the parameters have to be entered or else it won't be picked up.

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Try this as regex:
    Code:
    #\[image(?: align=(.*?))?(?: width=(.*?))?(?: height=(.*?))?\]#msi
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    New Coder
    Join Date
    Aug 2004
    Location
    UK
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Fantastic. Thank you very much.

    I would say it would be good if the order of the parameters could be changed as well but that may be asking a little too much as this does the job fine so long as users remember the correct order.

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I was bored...
    PHP Code:
    <?php
    $data 
    '[image height=542 width=542 align=left foo=bar]\n[image width=542 height=542]\n[image]';
    $valid_args = array('align''width''height');
    $preg '/(?>\[image)';
    for(
    $i 0$n count($valid_args); $i $n$i++)
    {
        
    $preg .= '(?: (.*?)=(.*?))?';
    }
    $preg .= '\]/sim';
    preg_match_all($preg$data$matchPREG_PATTERN_ORDER);
    for(
    $j 0$m count(array_shift($match)); $j $m$j++)
    {
        for(
    $i 0$n count($match); $i $n$i += 2)
        {
            if(empty(
    $match[$i][$j]))
            {
                unset(
    $match[$i][$j]);
            }
            if(
    in_array($match[$i][$j], $valid_args))
            {
                
    $args[$j][$match[$i][$j]] = $match[$i 1][$j];
                if(
    $i == $n 2)
                { 
    // You should validate the values in some way... a tab might be used but I don't wan't to use another regex for that.
                    
    $args[$j][$match[$i][$j]] = substr($match[$i 1][$j], 0strpos($match[$i 1][$j], ' '));
                }
            }
            else
            {
                unset(
    $match[$i][$j], $match[$i 1][$j]);
            }    
        }
    }
    print_r($args);
    ?>
    ... and it was a challenge
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

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