开发者

How can I translate "select from this variable to this variable only" into a code?

I have th开发者_开发知识库ree variables here.

$first= 'Start';
$second = 'End';
$testVar = 'Start here and here until the End more more more strings here';

How can i search $testVar if it contains Start and End strings, and i want to post Start up to the End string only.


Another way is using substr() and strops()

substr($testVar,strpos($testVar,$first),strpos($testvar,$second)+3)


Looking at your previous posts I'm assuming this is a PHP question. If so you can use:

list(,$result) = explode($first,$testVar);
list($result) = explode($second,$result);

Codepad link

You can also use regex as:

$first = preg_quote($first);
$second = preg_quote($second);

if(preg_match("!$first(.*?)$second!",$testVar,$m)) {
        echo $m[1];                  
}

Codepad link


I would also rather use strpos, strlen and substr:

$end_pos = strpos($testVar,$end)+strlen($end);
$result = substr($testVar, strpos($testVar,$start), $end_pos );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜