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 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. 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 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)); ?> 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! 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
Archived
This topic is now archived and is closed to further replies.