Jump to content

Striping text between certain characters


Texan78
Go to solution Solved by Frank_b,

Recommended Posts

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

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

  • Solution

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)');
Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

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.  

Link to comment
Share on other sites

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+/'.

Link to comment
Share on other sites

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 by hansford
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.