2014-01-11 34 views
0

我使用以下函数来查找字符串中的第n个字符,它运行良好。但是有一个例外,可以说其用于此目的的逗号,我需要改变这个是,如果昏迷中(和),那么它不应该指望的是查找第n个字符,除非它括在括号内php

function strposnth($haystack, $needle, $nth=1, $insenstive=0) 
{ 
    //if its case insenstive, convert strings into lower case 
    if ($insenstive) { 
     $haystack=strtolower($haystack); 
     $needle=strtolower($needle); 
    } 
    //count number of occurances 
    $count=substr_count($haystack,$needle); 

    //first check if the needle exists in the haystack, return false if it does not 
    //also check if asked nth is within the count, return false if it doesnt 
    if ($count<1 || $nth > $count) return false; 


    //run a loop to nth number of occurrence 
    //start $pos from -1, cause we are adding 1 into it while searching 
    //so the very first iteration will be 0 
    for($i=0,$pos=0,$len=0;$i<$nth;$i++) 
    { 
     //get the position of needle in haystack 
     //provide starting point 0 for first time ($pos=0, $len=0) 
     //provide starting point as position + length of needle for next time 
     $pos=strpos($haystack,$needle,$pos+$len); 

     //check the length of needle to specify in strpos 
     //do this only first time 
     if ($i==0) $len=strlen($needle); 
    } 

    //return the number 
    return $pos; 
} 

所以香港专业教育学院得到了正则表达式的工作(?)之外只能捕获逗号,即: '/,(?= [^)] *(?:[(] | $))/'

您可以看到一个在这里工作的实例: http://regex101.com/r/xE4jP8

但我不知道如何使它在strpos循环内工作,我知道我需要做什么,告诉它针有这个gex异常,但我不知道如何使其工作。也许我应该抛弃功能并使用另一种方法?

仅举我最终的结果,我想每6个逗号后的字符串分割的下一个字符串开始之前,例如:

rttr,ertrret,ertret(yes,no),eteert,ert ert,rtrter,0 rttr,ert(yes,no)rret,ert ret,eteert,ertert,rtrter,1 rttr,ertrret,ert ret,eteert,ertert,rtrter,0 rttr,ertrret,ert ret,eteert,ertert,rtrter,2 rttr,ert(white,black)rret,ert ret,eteert,ertert,rtrter,0 rttr,ertrret,ert ret,eteert,ertert,rtrter,0 rttr,ertrret,ert ret,et(blue,green)eert,ertert,rtrter,1 

注意,总有一个1位数字(1-3)并在字符串的下一部分开始之前的第六个逗号之后的空格,但我不能真正依靠它,因为它可能发生在字符串的早期可能发生,所以我总是可以依靠事实生病需要在第一个字符串之后拆分字符串第六个逗号后的数字和空格。所以我想在这之后直接分割字符串。

例如上面的字符串将被拆分这样的:

rttr,ertrret,ertret(yes,no),eteert,ert ert,rtrter,0 
rttr,ert(yes,no)rret,ert ret,eteert,ertert,rtrter,1 
rttr,ertrret,ert ret,eteert,ertert,rtrter,0 
rttr,ertrret,ert ret,eteert,ertert,rtrter,2 
rttr,ert(white,black)rret,ert ret,eteert,ertert,rtrter,0 
rttr,ertrret,ert ret,eteert,ertert,rtrter,0 
rttr,ertrret,ert ret,et(blue,green)eert,ertert,rtrter,1 

我能做到这一点我自己很容易,如果我知道如何让字符的位置,然后我可以使用SUBSTR分裂,但一更简单的方法可能使preg_split但林不知道如何将工作,直到我估计这部分出

我希望我不是在解释太混乱了,我敢打赌,我是:)

+0

当'nth'字符*被括在括号中时,你想要做什么? –

+0

忽略它,基本上发生了什么是总是有7个值,每个值用逗号分隔。然而,有时用户提交的文本存储在()中,可能有一个逗号,因为我无法控制它,所以当我拆分它时,我需要忽略()中的任何内容,所以我不会结束拆分错误的地方。其余的数据从来没有逗号,所以我可以有信心使用这种方法,希望清除它。所以你可以看到(是的,不是)(黑色,白色)等是用户输入,如果我计算了这些逗号,他们可能会弄乱分割。 – user1547410

回答

2

对于此类嵌套问题通常的正则表达式不是正确的工具。但是,如果问题实际上并不像你的那样复杂,那么正则表达式将会很好。

试试这个:

(?:^|,)((?:[^,(]*(?:\([^)]*\))?)*) 
^ start the search with a comma or the start of the string 
     ^start non capture group 
     ^search until comma or open parenthesis 
       ^if parenthesis found then capture until 
         ^end of parenthesis 
           ^end of capture group repeat if necessary 

看到它在行动:http://regex101.com/r/eS0cX4

正如你可以看到这将捕获的逗号括号外之间的一切。如果您使用preg_match_all将所有这些匹配项匹配到数组中,则可以按照您喜欢的方式将其分割。

+0

有趣的答案! –

相关问题