简体   繁体   English

找到第n个字符,除非它括在括号php中

[英]Find nth character except if its enclosed in brackets php

I use the following function to find the nth character in a string which works well. 我使用以下函数来查找字符串中的第n个字符,该字符串运行良好。 However there is one exception, lets say its a comma for this purpose, what i need to alter about this is that if the coma is within ( and ) then it shouldnt count that 然而有一个例外,让我们说它是一个逗号用于此目的,我需要改变的是,如果昏迷在(和)内,那么它不应该计算

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;
}

So ive got the regex working that only captures the comma when outside of () which is: '/,(?=[^)]*(?:[(]|$))/' 所以我得到的正则表达式工作只在()之外捕获逗号:'/,(?= [^]] *(?:[(] | $))/'

and you can see a live example working here: http://regex101.com/r/xE4jP8 您可以在这里看到一个实时示例: http//regex101.com/r/xE4jP8

but im not sure how to make it work within the strpos loop, i know what i need to do, tell it the needle has this regex exception but i am not sure how to make it work. 但我不知道如何让它在strpos循环中工作,我知道我需要做什么,告诉它针有这个正则表达式异常,但我不知道如何让它工作。 Maybe i should ditch the function and use another method? 也许我应该抛弃这个功能并使用另一种方法?

Just to mention my end result i want is to split the string after every 6 commas before the next string starts, example: 只是提到我想要的最终结果是在下一个字符串开始之前每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

Note that there is always a 1 digit number (1-3) and a space after the 6th comma before the next part of the string begins but i cant really rely on that as its possible earlier in the string this pattern could happen so i can always rely on the fact ill need to split the string after the first digit and space after the 6th comma. 请注意,在字符串的下一部分开始之前,总是有一个1位数字(1-3)和第6个逗号之后的空格但是我真的不能依赖于它,因为它可能在字符串的早期可能发生这样的模式,所以我可以总是依赖于在第6个逗号后面的第一个数字和空格后分割字符串的事实。 So i want to split the string directly after this. 所以我想在此之后直接拆分字符串。

For example the above string would be split like this: 例如,上面的字符串将像这样拆分:

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

I can do that myself pretty easily if i know how to get the position of the character then i can use substr to split it but an easier way might be preg_split but im not sure how that would work until i figure this part out 我可以很容易地做到这一点,如果我知道如何获得角色的位置然后我可以使用substr来分割它但更简单的方法可能是preg_split但我不知道如何才能工作,直到我想出这部分

I hope i wasnt too confusing in explaining, i bet i was :) 我希望我在解释时不会太混乱,我打赌我是:)

For these kind of nesting problems regex usually is not the right tool. 对于这些嵌套问题,正则表达式通常不是正确的工具。 However, when the problem is actually not that complicated, as yours seems to be, regex will do just fine. 然而,当问题实际上并不复杂时,就像你的问题一样,正则表达式会做得很好。

Try this: 尝试这个:

(?:^|,)((?:[^,(]*(?:\([^)]*\))?)*)
^ 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

See it in action: http://regex101.com/r/eS0cX4 请参阅以下内容: http//regex101.com/r/eS0cX4

As you can see this will capture everything between the comma's outside of the parenthesis. 正如您所看到的,这将捕获括号外的逗号之间的所有内容。 If you get all these matches into an array using preg_match_all you can split it any which way you like. 如果使用preg_match_all将所有这些匹配项放入数组中,则可以按照您喜欢的方式将其拆分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM