Texan78 Posted November 30, 2014 Share Posted November 30, 2014 This is an easy question for those fluid in PHP and one area I struggle in. I have this variable. $downloadedmetar1wind That outputs this.... S (180 degrees) at 26 MPH (23 KT) I am trying to strip away the (180 degrees) and the (23 KT) so it just reads S at 26 MPH This is what I have done but it isn't removing the brackets and the contents between it. $wind = preg_replace('/([\s.]\([0-9-]+\))/', '', $downloadedmetar1wind); -Thanks Quote Link to comment Share on other sites More sharing options...
mengo Posted November 30, 2014 Share Posted November 30, 2014 replace '[0-9-]+' with '[\w-]+', '[0-9-]' doesn't contain letter characters Quote Link to comment Share on other sites More sharing options...
Texan78 Posted November 30, 2014 Author Share Posted November 30, 2014 Thanks for the response. This is where I get thrown is which combination to use. That makes sense but, I am trying to remove the ( ) and both letters and numbers between that. I tried the suggestion but it doesn't remove anything. Is this correct? $wind = preg_replace('/([\s.]\([\w-]+\))/', '', $downloadedmetar1wind); Quote Link to comment Share on other sites More sharing options...
Solution Frank_b Posted November 30, 2014 Solution Share Posted November 30, 2014 function removeTextBetweenBrackets($str) { $inside = 0; $result = ''; for($i = 0 ; $i < strlen($str) ; $i++) { if($str[$i] == '(') $inside++; if($str[$i] == ')') $inside--; if($inside < 0) $inside = 0; if($inside == 0 && $str[$i] != ')') $result .= $str[$i]; } $arr = explode(' ', $result); $arr = array_map('trim', $arr); return implode(' ', array_filter($arr)); } echo removeTextBetweenBrackets('S (180 degrees) at 26 MPH (23 KT)'); Quote Link to comment Share on other sites More sharing options...
mengo Posted November 30, 2014 Share Posted November 30, 2014 of couse, it doesn't, because you missed a whitespace in '[\w-]' Quote Link to comment Share on other sites More sharing options...
Texan78 Posted November 30, 2014 Author Share Posted November 30, 2014 Thank you Frank_b that works great. I would have never came up with that. I was thinking I would need something simple but, that works too. Also thank you too mengo for taking the time as well. I am still curious about where I missed a whitespace. It looks the same as I posted in my example. -Thanks Quote Link to comment Share on other sites More sharing options...
hansford Posted November 30, 2014 Share Posted November 30, 2014 This is just another way of accomplishing the same task.. $downloadedmetar1wind = 'S (180 degrees) at 26 MPH (23 KT)'; $downloadedmetar1wind = preg_replace('/\s\(180 degrees\)/','',$downloadedmetar1wind); $downloadedmetar1wind = preg_replace('/\s\(23 KT\)/','', $downloadedmetar1wind); Quote Link to comment Share on other sites More sharing options...
Texan78 Posted November 30, 2014 Author Share Posted November 30, 2014 This is just another way of accomplishing the same task.. $downloadedmetar1wind = 'S (180 degrees) at 26 MPH (23 KT)'; $downloadedmetar1wind = preg_replace('/\s\(180 degrees\)/','',$downloadedmetar1wind); $downloadedmetar1wind = preg_replace('/\s\(23 KT\)/','', $downloadedmetar1wind); Thanks for sharing, I am always looking to learn new things. How would that work with with dynamic content though? $downloadedmetar1wind is the orignal variable that has the output that needs to be stripped. So wouldn't you create a new variable instead of using $downloadedmetar1wind? For example shouldn't it be. $wind1 = '$downloadedmetar1wind'; preg_replace is replace static content in your example. That would work fine if that is the only thing that would need to get stripped but, this is dynamic content and the output of $downloadedmetar1wind is never the same. Quote Link to comment Share on other sites More sharing options...
mengo Posted November 30, 2014 Share Posted November 30, 2014 I don't know whether you have noticed that your pattern has many errors. Firstly, there is no need to use a subpattern, the outside pair of parentheses can completely be droped. Secondly, if you want to strip spaces next to '(' and ')', you need to use \s+ at both start and end sides, '[\s.]' is another mistake. At last, the content within '(' and ')', there is space in string '180 degrees' so a character represent white space also exists in character class, you can use either ' ' or '\s'. So the right pattern should like this '/\s+\([\w\s-]\)\s+/' or '/\s+\([\w -]\)\s+/'. Quote Link to comment Share on other sites More sharing options...
hansford Posted November 30, 2014 Share Posted November 30, 2014 (edited) When using regular expressions to solve a problem, now you have two problems. It gets tricky because you need to know some basic pattern you can reliably count on so your expressions will work! If you know every time that you need to remove whatever is in parenthesis and the parenthesis, then this will work. Others might have a different approach. Test and retest whatever method you choose to see what works best for the given problem. $downloadedmetar1wind = 'S (2345678 degrees, whatever) at 26 MPH (3 5 7 9 11 FT,MPG,NBA) and 140/80 BP (Blood Pressure)'; $newWord = preg_replace('/(\s+\([^\)]*\))+/','',$downloadedmetar1wind); Edited November 30, 2014 by hansford Quote Link to comment 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.