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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度