skip to Main Content

I have this tag on an HTML code:

text html [button link="google.com" color="#fff" text="this text here"] rest of html

I wish i could have the parameters of this "button code" in a PHP variable, but have no idea how because of Regex.

I tried using preg_match_all but no success. Like this one:

preg_match_all('/color=(w*)/i', $text, $color);

Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    Thanks guys. I will post the result that worked for me. Maybe it helps someone else! The options are already in my language, portuguese. But you can understand!

    I needed the script to solve the codes for more than one tag in the same HTML.

    $matches = [];
            preg_match_all('/[botao link={(.*?)} texto={(.*?)} corfundo={(.*?)} corletra={(.*?)}]/', $text, $matches);
            
            foreach($matches[0] as $key => $match) {
                $link = $matches[1][$key];
                $texto = $matches[2][$key];
                $corfundo = $matches[3][$key];
                $corletra = $matches[4][$key];
            
                $text = str_replace($match, '<a target="_blank" title="'.$texto.'" class="botao" href="'.$link.'" style="background-color: '.$corfundo.'; color: '.$corletra.';">'.$texto.'</a>', $text);
            }
    

  2. You could use preg_match_all():

    <?php
    
    $text = 'text html [button link="google.com" color="#fff" text="this text here"] rest of html';
    preg_match_all('/[button link="(.*?)" color="(.*?)" text="(.*?)"]/i', $text, $matches);
    $link = $matches[1][0];
    $color = $matches[2][0];
    $text = $matches[3][0];
    
    echo $link;
    echo $color;
    echo $text;
    

    Output:

    google.com
    #fff
    this text here
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search