Hello,
I am trying to write a function to get the string between two other strings in a string of text.
This is what I have so far:

	function retrieveBetween($subject, $start, $end) {
		$matchPattern = "/.*$start.*$end.*/";
		if (preg_match($matchPattern, $subject, $matches)) {
			return $matches[0];
		} else {
			return "Could not find valid match.";
		}
	}

However it keeps returning "could not find valid match."
I dont think I have a proper handle on regular expressions yet.
Could someone please tell me what is wrong with this code?

Thanks.

    Try $matchPattern = "/$start(.*)$end/"

    and return $matches[1]

      Hm okay I did that and it works well for some strings but other ones (that use special characters) fail... example:

      //fails
      echo retrieveBetween("my what a [[good day it[] is today!", "[[", "[]");
      

        You need to escape the [ s

        echo retrieveBetween("my what a [[good day it[] is today!", "\[\[", "\[]");

          HalfaBee wrote:

          You need to escape the [ s

          And you can use [man]preg_quote[/man] to do that.

            Thread resolved. I used a little snippet I found on the net.

            Could come in handy for someone:

            	function get_string_between($string, $start, $end){
            	        $string = " ".$string;
            	        $ini = strpos($string,$start);
            	        if ($ini == 0) return "";
            	        $ini += strlen($start);   
            $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len); }

              That was obviously written by someone who doesn't know how to use [man]strpos[/man] to see if a substring exists (the code given risks bugs if for some wacky reason you want $start to start with a space).
              You can replace the first three lines with

              $ini = strpos($string, $start);
              if($ini===false) return "";
              

              But I was bored: I thought I'd find a different way to do it. Without strpos or substr.

              function get_string_between($string, $start, $end){
              	$chunk = explode($start, $string, 2);
              	if(count($chunk)==1) return "";
              	$chunk = explode($end, $chunk[1], 2);
              	if(count($chunk)==1) return "";
              	return $chunk[0];
              }
              

                That was obviously written by someone who doesn't know how to use [man]strpos[/man] to see if a substring exists (the code given risks bugs if for some wacky reason you want $start to start with a space).
                You can replace the first three lines with

                $ini = strpos($string, $start);
                if($ini===false) return "";
                

                But I was bored: I thought I'd find a different way to do it. Without strpos or substr.

                function get_string_between($string, $start, $end){
                	$chunk = explode($start, $string, 2);
                	if(count($chunk)==1) return "";
                	$chunk = explode($end, $chunk[1], 2);
                	if(count($chunk)==1) return "";
                	return $chunk[0];
                }
                
                  Write a Reply...