开发者

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

开发者 https://www.devze.com 2023-01-25 02:09 出处:网络
I have th开发者_开发知识库ree variables here. $first= \'Start\'; $second = \'End\'; $testVar = \'Start here and here until the End more more more strings here\';

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

精彩评论

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