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 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 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 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; ?> 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; ?> 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" 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. 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'; } 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 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 Link to comment https://forums.phpfreaks.com/topic/117991-solved-substr/#findComment-607029 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.