这个{#....#}之间的正则表达式preg_match

时间:2014-10-09 16:21:11

标签: php preg-match

我想在这个2字符串开始之间创建一个preg_match" {#"并在此结束"#}";

在这之间提取数据的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

"/\{\#(.*?)\#\}/g"

另请在此处学习正则表达式http://regex101.com/


修改:

你会分享一些代码,因为它运行正常。 这是对正则表达式的测试

http://regex101.com/r/aC1fT2/1

在这里它正在发挥作用:

 ~ 
 ⚡⚡⚡  more test.php; php test.php 
<?php
//The source code
    $pattern = "/\{\#(.*?)\#\}/";
    $test_string = 
    "
        Lorem Ipsum is simply {#dummy#} text 
        of the printing and typesetting industry. 
        Lorem Ipsum has been the industry's {#standard#} dummy text 
        ever since the 1500s, 
        when an unknown printer took a galley of type 
        and scrambled it to make a type specimen book. 
        It has survived not only five centuries, 
        but also the leap into electronic typesetting, 
        remaining {#essentially unchanged#}. 
        It was popularised in the 1960s 
        with the release of Letraset sheets 
        containing Lorem Ipsum passages, 
        {#and more recently with desktop publishing software#} 
        like Aldus PageMaker including versions of Lorem Ipsum.
    ";
    preg_match_all($pattern, $test_string, $matches);
    print_r($matches);
?>
...
//The output
Array
(
    [0] => Array
        (
            [0] => {#dummy#}
            [1] => {#standard#}
            [2] => {#essentially unchanged#}
            [3] => {#and more recently with desktop publishing software#}
        )

    [1] => Array
        (
            [0] => dummy
            [1] => standard
            [2] => essentially unchanged
            [3] => and more recently with desktop publishing software
        )

)