永远不会结束使用正则表达式的do-while循环

时间:2016-11-17 14:34:32

标签: php regex do-while pcre regex-lookarounds

我正在尝试在XLIFF文件上运行一系列模式。样本:

  <trans-unit id="1">
    <source> I like "sausages". </source>
    <target> J'aime bien les « sausices » </target>
  </trans-unit>
  <trans-unit id="2">
    <source> I like "sausages". </source>
    <target> J'aime bien les «sausices» </target>
  </trans-unit>

我解析文件,然后在每个目标元素上运行每个模式。

    foreach($patterns as $p) {
        if (preg_match($p['find'], $tu[0]->target, $dummy)) {
            do {
                $targetText = $tu[0]->target;
                $tu[0]->target = preg_replace($p['find'], $p['repl'], $targetText, -1, $count);
            } while ($count);
        }
    }

例如,我有一个带有图案的数组:

        $patterns[1] = array(
            'find' => "/[«‹]\K(?!\x{00A0})\s/imu",
            'repl' => "&#8239;"
            );
        $patterns[2] = array(
            'find' => "/[«‹]\K(?!\p{Zs})/imu",
            'repl' => "&#8239;"
            );

模式1应与上面的反式单元1匹配,模式2应与反式单元2匹配。模式1工作正常,但如果我运行模式2(仅限或两者),则循环永远不会结束。替换基本上取代了«或<(模式1)之后的正常(断开)空间和狭窄的空间,或者如果根本没有空间(模式1)则插入它。

我想说这个问题与第二个正则表达式有关,但我无法弄清楚该表达式有什么问题。有什么提示吗?

1 个答案:

答案 0 :(得分:1)

// fix share facebook open graph // //Adding the Open Graph in the Language Attributes function add_opengraph_doctype( $output ) { return $output . ' xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"'; } add_filter('language_attributes', 'add_opengraph_doctype'); //Lets add Open Graph Meta Info function insert_fb_in_head() { global $post; if ( !is_singular()) //if it is not a post or a page return; echo '<meta property="fb:admins" content="YOUR USER ID"/>'; echo '<meta property="og:title" content="' . get_the_title() . '"/>'; echo '<meta property="og:type" content="article"/>'; echo '<meta property="og:url" content="' . get_permalink() . '"/>'; echo '<meta property="og:site_name" content="KALTARA CENTER"/>'; if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image $default_image="https://scontent-sin6-1.xx.fbcdn.net/v/t1.0-9/12743574_1707554466145558_2816765664534740995_n.jpg?oh=e9bac3fd82d2717505136a02c083045e&oe=588CFEF4"; //replace this with a default image on your server or an image in your media library echo '<meta property="og:image" content="' . $default_image . '"/>'; } else{ $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' ); echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>'; } echo " "; } add_action( 'wp_head', 'insert_fb_in_head', 5 ); the_post_thumbnail(array(200,200), array('class' => 'alignleft')); 模式与\p{Zs}不匹配,因此在第二种模式中向先行条件添加&#8239;

&#8239;