simonp Posted October 22, 2009 Share Posted October 22, 2009 Hi folks, I have a string that looks like this: aaaaaaaaaa: bbbbbbbbbb (ccccccccc) ddddddddddddddddddddd eeeeeeeeeeeeeeeeeeeee (format can't be changed - lengths of strings will vary) and I need to extract the c's that are inside the brackets. How could I do this? Cheers Simon Quote Link to comment https://forums.phpfreaks.com/topic/178595-solved-extract-text-from-string/ Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 preg_match('#\(([^)]+)\)#', $string, $matches); Should do it. See the contents of $matches. Quote Link to comment https://forums.phpfreaks.com/topic/178595-solved-extract-text-from-string/#findComment-941911 Share on other sites More sharing options...
JAY6390 Posted October 22, 2009 Share Posted October 22, 2009 preg_match('%\(([^\)]+)%', $input, $matches); echo $matches[1]; Edit: Too slow Quote Link to comment https://forums.phpfreaks.com/topic/178595-solved-extract-text-from-string/#findComment-941913 Share on other sites More sharing options...
JonnoTheDev Posted October 22, 2009 Share Posted October 22, 2009 Without regex <?php $string = "aaaaaaaaaa: bbbbbbbbbb (ccccccccc) ddddddddddddddddddddd eeeeeeeeeeeeeeeeeeeee"; $string = substr($string,0,strpos($string,")")); print substr($string, strpos($string,"(")+1, strlen($string)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/178595-solved-extract-text-from-string/#findComment-941914 Share on other sites More sharing options...
simonp Posted October 22, 2009 Author Share Posted October 22, 2009 Thanks everyone - you're brilliant! Quote Link to comment https://forums.phpfreaks.com/topic/178595-solved-extract-text-from-string/#findComment-941922 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.