dqfwcj0030 2014-08-14 18:22
浏览 43
已采纳

php - regex / preg_match_all(子)短代码属性

I try to regex/preg_match_all shortcodes (in wordpress) in order to get a specific attribute value, however my php code partially works...

In fact I success to regex parent shortcodes but not children shortcodes.

My shortcode looks like to this:

[to_custom_font family="Lato;900italic" decoration="" style="normal"]Content[/to_custom_font]

Here my php code:

preg_match_all("/$pattern/",$post_content,$matches);
$to_shortcode = array_keys($matches[2],'to_custom_font');
if (!empty($to_shortcode)) {
    foreach($to_shortcode as $sc) {
        preg_match('/family="([^"]+)"/', $matches[3][$sc], $match);
        $font_infos = explode(';',$match[1]);
        $family     = $font_infos[0];
        $variant    = $font_infos[1];
        $font       = $family.':'.$variant;

        if(!in_array($font, $available_families)){
            $available_font = array_merge($available_font, array($font => $post_id));
        }

    }
}

It works with parent shortcodes but not with children shortcodes:

[to_custom_font family="Lato;900italic" decoration="" style="normal"]Content[/to_custom_font] //parent shortcode

[to_section attr="" attr3=""]
[to_custom_font family="Lato;900italic" decoration="" style="normal"]Content[/to_custom_font]//child shortcode
[/to_section]

It seems that the problem comes from here :

preg_match_all("/$pattern/",$post_content,$matches);

$matches only return parent shortcodes. And I need to get all children levels...

My aim with this code is to get all value family="" attribute. Maybe there is a better way to do it...

  • 写回答

1条回答 默认 最新

  • dqoag62688 2014-08-14 18:55
    关注

    If I understand your question, this might be what you need

    $string = '[to_custom_font family="Lato;900italic" decoration="" style="normal"]Content[/to_custom_font] //parent shortcode[to_section attr="" attr3=""][to_custom_font family="Lato;900italic" decoration="" style="normal"]Content[/to_custom_font]//child shortcode[/to_section]';
    
    preg_match_all('/family="([^"]+)"/', $string, $matches);    
    foreach ($matches[1] as $match) {
        echo $match . "
    ";
    }
    
    // or use print_r() to see the whole array
    
    print_r($matches);
    
    ?>
    

    Output:

    Lato;900italic
    Lato;900italic
    
    Array
    (
        [0] => Array
            (
                [0] => family="Lato;900italic"
                [1] => family="Lato;900italic"
            )
    
        [1] => Array
            (
                [0] => Lato;900italic
                [1] => Lato;900italic
            )
    
    )
    

    PHP demo | Regex demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题