1

Why doesn't this work?

<?php

function GetBetween($var1="",$var2="",$pool) {

    $temp1 = strpos($pool,$var1)+strlen($var1);
    $result = substr($pool,$temp1,strlen($pool));
    $dd=strpos($result,$var2);

    if($dd == 0) {
         $dd = strlen($result);
    }

    return substr($result,0,$dd);
}

$str = "[id]159828[/id][name][/name]";

$str =  GetBetween("[name]","[/name]",$str);

echo $str;
?>

Returns:

[/name]

Works fine if there is something in there. I need it to return NOTHING if its not found.

Thanks!

2
  • If nothing is between those fields $dd is first equal to 0 and then changed to be the length of the result. This makes your final call substr($result,0,7). If you have a value between the name brackets $dd is never set to 0 and the secondary assignment does not happen. Get rid of the if($dd == 0)... block. Commented Aug 3, 2013 at 1:32
  • No use setting default values for $var1 and $var2 if $pool is a required parameter of GetBetween(). Put $pool as the first parameter if you want to make the others optional. Commented Aug 3, 2013 at 1:50

4 Answers 4

1

Everything works fine up until $result. You have:

$result = substr($pool,$temp1,strlen($pool));

We have $temp1 = 15, and strlen($pool) = 22, so this is equivalent to:

$result = substr("[id]159828[/id][name][/name]", 15, 22)

You may want to omit the $length argument here, so that substr just returns the remainder of the string after position 15. Either way, $result is equal to "[/name]".

This means that $dd is going to equal 0, since the strpos of "[/name]" in $result is at the very beginning. The if() statement then changes $dd to strlen($result), which is 7. Then, the return value is substr($result,0,$dd), or $substr("[/name]",0,7), which evaluates to "[/name]".

To fix this, you need to remove the if() statement entirely.

That way, $dd is left at zero, so the return value is a substring of zero length, which is what you wanted: an empty string.

Sign up to request clarification or add additional context in comments.

Comments

1

just add this check:

if(substr($pool, strpos($pool, $var1), strlen("$var1$var2")) == "$var1$var2")
return false;

Then:

function GetBetween($var1="",$var2="",$pool) {
    if(substr($pool, strpos($pool, $var1), strlen("$var1$var2")) === "$var1$var2"){
      return false;
    }
    $temp1 = strpos($pool,$var1)+strlen($var1);
    $result = substr($pool,$temp1,strlen($pool));
    $dd=strpos($result,$var2);

    if($dd == 0) {
         $dd = strlen($result);
    }

    return substr($result,0,$dd);
}

Comments

1

You can use regular expressions to match the stuff between two values. For example

function between($v1, $v2, $str){
    $v1 = preg_quote($v1, '~');
    $v2 = preg_quote($v2, '~');
    if(preg_match("~$v1(.*)$v2~", $str, $tmp)){
       return $tmp[1];
    }else{
       return "";
    }
}

Or even without empty return since $tmp[1] will be null if there is no match.

Comments

0

If you're always parsing the same format, you can try a sscanf()

It's quite nice for defining a format then reading variables out without using regular expression matching.

eg.

$str = "[id]159828[/id][name][/name]";
sscanf($str, "[id]%d[/id][name]%[^[/name]]", $id, $name);

$id and $name will be populated with the (even empty) values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.