dowb58485 2013-09-12 13:11
浏览 23
已采纳

substring在两个单词之间找到

I just want to get the string followed by extension .html from the below string.

the example :

login.html

forgot_password.html

admin_screen.html

delete_checklist.html

print_preview.html

String:

 $haystack= "b='rootNodes',c='pageName',d='login',e='type',f='kjhk',g='url',h='login.html',i='children'
    ,j='forgot password',k='forgot_password.html',l='password recovery',m='password_recovery.html',n='superadmin dashboard',o='superadmin_dashboard.html',p='admin screen',q='admin_screen.html',r='newchecklist
    popup',s='admin_screen.html',t='delete checklist',u='delete_checklist.html',v='print preview', w='print_preview.html',x='Checklist - Normal Procedure',y='Checklist___Normal_Procedure.html',
    z='normal procedure - insert text'"

I have tried but result is not near.

    function strallpos($haystack,$needle,$offset = 0){
        $result = array();
        for ($i = $offset; $i < strlen($haystack); $i++) {
            echo $pos = strpos($haystack, $needle, $i);
            if ($pos !== FALSE) {
                $offset = $pos;
                if ($offset >= $i) {
                    $i = $offset;
                    $pos2 = strpos($haystack, '=', $i);

                    //Here substr find

                    $result[] = $offset;
                }
            }
        }
    return $result;
    }
  • 写回答

4条回答 默认 最新

  • dongraobei6719 2013-09-12 13:21
    关注

    Using PCRE Assertion (lookbehind):

    $string = 'b="rootNodes",c="pageName",d="login",e="type",f="kjhk",g="url",h="login.html",i="children"
        ,j="forgot password",k="forgot_password.html",l="password recovery",m="password_recovery.html",n="superadmin dashboard",o="superadmin_dashboard.html",p="admin screen",q="admin_screen.html",r="newchecklist
        popup",s="admin_screen.html",t="delete checklist",u="delete_checklist.html",v="print preview", w="print_preview.html",x="Checklist - Normal Procedure",y="Checklist___Normal_Procedure.html",
        z="normal procedure - insert text"';
    
    $pattern = '#\w+(?=\.html)#s';
    preg_match_all($pattern, $string, $matches);
    print_r($matches);
    
    Array
    (
        [0] => Array
            (
                [0] => login
                [1] => forgot_password
                [2] => password_recovery
                [3] => superadmin_dashboard
                [4] => admin_screen
                [5] => admin_screen
                [6] => delete_checklist
                [7] => print_preview
                [8] => Checklist___Normal_Procedure
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程