0

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

这个例子 :

登录.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

4 回答 4

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);
于 2013-09-12T13:17:14.333 回答
1

使用 PCRE 断言(向后看):

$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
        )

)
于 2013-09-12T13:21:21.787 回答
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>';
    }
于 2013-09-12T13:21:46.727 回答
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>';
    }
}  

结果 ...

登录.html
forgot_password.html
密码恢复.html
superadmin_dashboard.html
admin_screen.html
admin_screen.html
delete_checklist.html
print_preview.html
清单___Normal_Procedure.html

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

于 2013-09-12T13:54:47.683 回答