【问题标题】:What's wrong with preg_match_all and my string?preg_match_all 和我的字符串有什么问题?
【发布时间】:2019-08-27 16:48:16
【问题描述】:

我在文本中有一系列标签,我想与preg_match_all 匹配,但$regex 字符串有问题,我不明白该怎么办。

$tagReplace = ['mov', 'flv', 'youtube'];
foreach ($tagReplace as $plg_tag) {
    $regex = "#{" . $plg_tag . "}(.*?){/" . $plg_tag . "}#s";
    $var = preg_match_all($regex, "{mov}the_video_url{/mov}", $matches, PREG_PATTERN_ORDER);
    var_dump($matches);
}

【问题讨论】:

  • 我想用 preg_match_all 替换它不会改变字符串
  • 第一行有语法错误$tagReplace = [mov, flv, youtube];
  • mov、flv、youtube 是常量吗?
  • foreach ($tagReplace as $plg_tag) - 因为现在你找到 0,1,2 而不是 mov, flv...
  • @splash58 我已经更正了问题

标签: php regex preg-match-all


【解决方案1】:
<?php

 $matches = array();
 $p = '/(mov|flv|youtube)/';
 $replace_to = 'your text you want to be insted of the mov or flv or youtube';
 $str = ' the string that you want to work on and replace its content';

$p_c = array(
             $p => function($match) {
                   $ret = str_replace("mov", $replace_to, $match[0]);
                   $ret = str_replace("flv", $replace_to, $ret);
                   $ret = str_replace("youtube", $replace_to, $ret);

                   return $ret;
             }
 );

 $res3 = preg_replace_callback_array($p_c, $str);

【讨论】:

    猜你喜欢
    • 2014-07-15
    • 2016-01-20
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多