如何提取字符串

时间:2016-03-21 16:37:28

标签: php string explode substr

我在PHP中有以下字符串。

"File:content/resources/11933/Calm_Nebula_by_The_Free_Mason.jpgDate:Fri Mar 18 11:30:17 CET 2016Size:23124"

我需要分别获取文件,日期和大小。

提取所需值的最佳方法是什么?

explode() OR substring()

爆炸需要一些共同的价值,子串需要开始和结束。在这两种情况下我都有点混乱。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:4)

试试这个:

<?php
$text = "File:content/resources/11933/Calm_Nebula_by_The_Free_Mason.jpgDate:Fri Mar 18 11:30:17 CET 2016Size:23124";
$pattern = '/file:(.*?)date:(.*?)size:(.*)/i';
preg_match($pattern, $text, $out);
print_r($out);
?>

输出:

Array
(
    [0] => File:content/resources/11933/Calm_Nebula_by_The_Free_Mason.jpgDate:Fri Mar 18 11:30:17 CET 2016Size:23124 
    [1] => content/resources/11933/Calm_Nebula_by_The_Free_Mason.jpg
    [2] => Fri Mar 18 11:30:17 CET 2016
    [3] => 23124
)

演示:https://3v4l.org/OcSXH