Hello, I am trying to find the value of the text between square brackets in my string $str = "hell my name is [andrew]"; $find = preg_split("\[(*])\]", $str, null, 1); $name = trim($find[1]); Code (markup): However this is not working :'( Anyone got any ideas?
look at this post http://forums.digitalpoint.com/showthread.php?t=1421903 or use this function 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); } $str = "hell my name is [andrew]"; echo get_string_between($str, "[", "]"); PHP:
This could be resolved using regular expressions, we dont need to use dump coder technnics here. You should use preg_match, is more easy that way: $str = "hell my name is [andrew]"; preg_match("/^(.*\[)(.*)(\])/", $str, $find); $name = trim($find[2]); Code (markup): You should add validations, for example to handle when the string doesnt have any []. I hope this helps. Gustavo