English 中文(简体)
PHP 指数前后的扼杀首先发生
原标题:PHP First occurance of string before and after index
  • 时间:2012-04-11 17:01:51
  •  标签:
  • php
  • string

页: 1 我的索引编号为9(此处为e),我将不得不在两个之间打上。 我不知道第一点点离指数有多远,也不知道第二点是多少。 因此,在营地中这样做是没有任何容易的方法? 另有许多<代码>:s in the string ex. a.b.c.d.something.d.c.b.a

其他几个例子:

bef.ore.something.af.t.erindex: 12 = someth

bef.ore.something.af.t.erindex: 5 = ore<>code>

bef.ore.something.af.t.erindex: 19 = af

最佳回答

作为起点,你可以尝试:

$input =  a.b.something.c.def ;
$index = 9;
$delimiter =  . ;

/*
 * get length of input string
 */
$len = strlen($input);

/*
 * find the index of the first delimiter *after* the index
 */
$afterIdx = strpos($input, $delimiter, $index);

/*
 * find the index of the last delimiter *before* the index 
 * figure out how many characters are left after the index and negate that - 
 * this makes the function ignore that many characters from the end of the string,
 * effectively inspecting only the part of the string up to the index
 * and add +1 to that because we are interested in the location of the first symbol after that
 */
$beforeIdx = strrpos($input, $delimiter, -($len - $index)) + 1; 

/*
 * grab the part of the string beginning at the last delimiter 
 * and spanning up to the next delimiter
 */
$sub = substr($input, $beforeIdx, $afterIdx - $beforeIdx);
echo $sub;

注:至少,如果在指数之前/之后没有文号,则需要增加一些疗效检查。

问题回答

定期表达是你的朋友:

$regex =  /.?([w]+)/ ;
$string =  a.b.c.d.something.d.c.b.a ;
preg_match_all($regex, $string, $result);
print_r($result[1]);

Note: If your looking for a specific word, just replace [w]+ with the word your looking for.

@19greg96 我看到你现在想要做些什么,对DCoder的榜样采取的一种替代但简单的做法就是:

$string =  a.b.something.d.c.b.a ;
$index = 9;
$delimiter =  . ;

$last_index = strpos($string, $delimiter, $index);
$substr = substr($string, 0, $last_index);
$substr = substr($substr, strrpos($substr, $delimiter) + 1);
echo $substr;

挖掘成阵列,并通过旁边传:

$str= a.b.c.d.something.d.c.b.a ;
$parts=explode( . ,$str);
foreach($parts as $part) {
    if($part== something ) {
        echo( Found it! );
        break;
    }
}




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...