dongzipu7517 2013-03-19 20:28
浏览 31
已采纳

PHP在一个输入中分隔两个不同的部分

I'm working on a PHP based application extension that will extend a launcher style app via the TVRage API class to return results to a user wherever they may be. This is done via Alfred App (alfredapp.com).

I would like to add the ability to include show name followed by S##E##: example: Mike & Molly S01E02

The show name can change, so I can't stop it there, but I want to separate the S##E## from the show name. This will allow me to use that information to continue the search via the API. Even better, if there was a way to grab the numbers, and only the numbers between the S and the E (in the example 01) and the numbers after E (in the example 02) that would be perfect.

I was thinking the best function is strpos but after looking closer that searches for a string within a string. I believe I would need to use a regex to correctly do this. That would leave me with preg_match. Which led me to:

$regex = ?;
preg_match( ,$input);

Problem is I just don't understand Regular Expressions well enough to write it. What regular expression could be used to separate the show name from the S##E## or get just the two separate numbers?

Also, if you have a good place to teach regular expressions, that would be fantastic.

Thanks!

  • 写回答

2条回答 默认 最新

  • doomm4711 2013-03-19 21:38
    关注

    Regex:

    $text = 'Mike & Molly S01E02';
    preg_match("/(.+)(S\d{2}E\d{2})/", $text, $output);
    print_r($output);
    

    Output:

    Array
    (
        [0] => Mike & Molly S01E02
        [1] => Mike & Molly 
        [2] => S01E02
    )
    

    If you want the digits separately:

    $text = 'Mike & Molly S01E02';
    preg_match("/(.+)S(\d{2})E(\d{2})/", $text, $output);
    print_r($output);
    

    Output:

    Array
    (
        [0] => Mike & Molly S01E02
        [1] => Mike & Molly 
        [2] => 01
        [3] => 02
    )
    

    Explanation:

    . --> Match every character

    .+ --> Match every character one or more times

    \d --> Match a digit

    \d{2} --> Match 2 digits

    The parenthesis are to group the results.

    www.regular-expressions.info is a good place to learn regex.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据