substring在两个单词之间找到

时间:2013-09-12 13:11:07

标签: php

我只想从下面的字符串中获取字符串后跟扩展名.html

示例:

的login.html

forgot_password.html

admin_screen.html

delete_checklist.html

print_preview.html

字符串:

 $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'"

我试过但结果并不近。

    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 个答案:

答案 0 :(得分:2)

正则表达式?

$s = ' 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"';
$n=preg_match_all('/([^"]+\.html)/',$s,$m);
echo var_export($m,true);

答案 1 :(得分:1)

使用PCRE断言(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
        )

)

答案 2 :(得分:1)

试试这个...我认为它有效

    $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"';

    $string = explode(',', $string);
    for ($i=0; $i < count($string); $i++) {
        $verf = strrpos($string[$i], '.html');
        if ($verf)
            $pos[] = $verf;
    }

    for ($i=0; $i < count($pos); $i++) {
        echo "index: ".$pos[$i].'<br>';
    }

答案 3 :(得分:1)

这是一个有效的爆炸:

<?php
$a = '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"';

$ex = explode ( ',' , $a );
foreach ($ex as $pair) {
    $str = str_replace('"', '', $pair);
    $ex2 = explode('=', $str);
    if (substr($ex2[1], -5) === '.html') {
        echo $ex2[1] . '<br>';
    }
}  

结果......

login.html
forgot_password.html
password_recovery.html
superadmin_dashboard.html
admin_screen.html
admin_screen.html
delete_checklist.html
print_preview.html
Checklist___Normal_Procedure.html

您可以将其放入数组中,而不是回显它。