jasonc Posted February 11, 2009 Share Posted February 11, 2009 I have the following string format and wish to get just the part within the brackets. $string = "something (this)"; $result = "this"; what is the best way to get this part of the string? Link to comment https://forums.phpfreaks.com/topic/144755-get-part-of-a-string-within-the-brackets/ Share on other sites More sharing options...
gevans Posted February 11, 2009 Share Posted February 11, 2009 Is the bracketed text always going to be at the end of the string? If so it may be faster to use a couple of String Functions, otherwise preg_match, a PCRE Function would be better. Link to comment https://forums.phpfreaks.com/topic/144755-get-part-of-a-string-within-the-brackets/#findComment-759569 Share on other sites More sharing options...
jasonc Posted February 11, 2009 Author Share Posted February 11, 2009 i have taken a look at the preg_replace as i wish to echo the string part on the page. i have taken a bit of code i have for another string that i have on the page and thought if i replaced the A-Z and replaced it with () it would also work, i dont understand how this should work. substr(preg_replace("/([()])/",',\\1',$string),1) the string could be many formats... "something (this)" "somthing (this) something else" but it is just the part within the () that is needed. Link to comment https://forums.phpfreaks.com/topic/144755-get-part-of-a-string-within-the-brackets/#findComment-759576 Share on other sites More sharing options...
gevans Posted February 11, 2009 Share Posted February 11, 2009 This should work for you; $test_string = 'somthing (this) something else'; $pattern = "/\(([^\)]*)\)/"; preg_match($pattern, $test_string, $matches); echo $matches[1]; Link to comment https://forums.phpfreaks.com/topic/144755-get-part-of-a-string-within-the-brackets/#findComment-759583 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.