woolyg Posted August 3, 2008 Share Posted August 3, 2008 Hi all, Is there a way to locate a string within a string that is definitely located between a set of characters? $str = "My Name is (WOOLYG)"; ..basically, I want to capture all of the characters between the brackets. Is this easily done? Thanks, WoolyG Quote Link to comment https://forums.phpfreaks.com/topic/117991-solved-substr/ Share on other sites More sharing options...
cooldude832 Posted August 3, 2008 Share Posted August 3, 2008 regex can match a pattern, however if u know the location of the ( and the ) is the last character you can use substr Quote Link to comment https://forums.phpfreaks.com/topic/117991-solved-substr/#findComment-607007 Share on other sites More sharing options...
woolyg Posted August 3, 2008 Author Share Posted August 3, 2008 That's the clincher though, I should have mentioned it - the string between the brackets could be any length.. any ideas? WoolyG Quote Link to comment https://forums.phpfreaks.com/topic/117991-solved-substr/#findComment-607010 Share on other sites More sharing options...
MadTechie Posted August 3, 2008 Share Posted August 3, 2008 regex example <?php $str = "My Name is (WOOLYG)"; if (preg_match('/\(([^)].*?)\)/si', $str, $regs)) { $result = $regs[1]; } else { $result = ""; } echo $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/117991-solved-substr/#findComment-607011 Share on other sites More sharing options...
cooldude832 Posted August 3, 2008 Share Posted August 3, 2008 try <?php $string = "My Name is (WOOLYG)"; $name = substr($name, strpos($string,"(")+1, (strlen($name)-strpos(strpos($string,"(")-1) ); echo $name; ?> Quote Link to comment https://forums.phpfreaks.com/topic/117991-solved-substr/#findComment-607013 Share on other sites More sharing options...
woolyg Posted August 3, 2008 Author Share Posted August 3, 2008 Hmm, no joy there, I'm getting "Parse error: syntax error, unexpected ';' in C:\Program Files\xampp\htdocs\paw\str.php on line 3" Quote Link to comment https://forums.phpfreaks.com/topic/117991-solved-substr/#findComment-607018 Share on other sites More sharing options...
MasterACE14 Posted August 3, 2008 Share Posted August 3, 2008 I've just tried it like this: <?php $string = "My Name is (WOOLYG)"; $name = substr($name,strpos($string,"(")+1,(strlen($name)-strpos(strpos($string,"(")-1))); echo $name; ?> and am getting... Warning: Wrong parameter count for strpos() in C:\Program Files\Abyss Web Server\htdocs\tests\stringmatch.php on line 3 the code was missing 1 bracket at the end. Quote Link to comment https://forums.phpfreaks.com/topic/117991-solved-substr/#findComment-607022 Share on other sites More sharing options...
The Little Guy Posted August 3, 2008 Share Posted August 3, 2008 This works (I tested it): if(preg_match("~\((.+?)\)~","My Name is (WOOLYG)",$matches)){ echo $matches[1]; }else{ echo 'No match'; } Quote Link to comment https://forums.phpfreaks.com/topic/117991-solved-substr/#findComment-607024 Share on other sites More sharing options...
MasterACE14 Posted August 3, 2008 Share Posted August 3, 2008 ah, sweet. @little guy. you got a typo in your signature Mean, Medaian, Mode should be Mean, Median, Mode Quote Link to comment https://forums.phpfreaks.com/topic/117991-solved-substr/#findComment-607027 Share on other sites More sharing options...
woolyg Posted August 3, 2008 Author Share Posted August 3, 2008 Great stuff everyone, thanks a million for your input - The Little Guy wins by a nose! Solved thanks - WoolyG Quote Link to comment https://forums.phpfreaks.com/topic/117991-solved-substr/#findComment-607029 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.