JChilds Posted June 10, 2008 Share Posted June 10, 2008 I have a list of Offices, and which level of a building they are on. Eg: Secretary Office (floor 12) Employee Gym (floor 7) I want to extract particular things. in this example, I want to know which level the Secretary Office is on. I thought I would try: preg_match_all("/Secretary Office (floor [1-9]?\d+/",$input,$secretary); $secretary[$x] = ltrim($secretary[$x],'Secretary Office (floor '); But it's having trouble with the ( before 'level' Giving the error: Compilation failed: missing ) Any idea how I can get around this problem? Quote Link to comment Share on other sites More sharing options...
JChilds Posted June 10, 2008 Author Share Posted June 10, 2008 Now apparently I can use : preg_match_all("^Secretary\ Office\ \(floor\ [-+]?\d+\)$",$input,$secretary); But for the love of me I can't get it to work... Quote Link to comment Share on other sites More sharing options...
JChilds Posted June 10, 2008 Author Share Posted June 10, 2008 anyone? Quote Link to comment Share on other sites More sharing options...
effigy Posted June 10, 2008 Share Posted June 10, 2008 Parens are metacharacters that must be escaped if you want to match a literal. <pre> <?php $data = <<<DATA Secretary Office (floor 12) Employee Gym (floor 7) DATA; preg_match_all('/^([^()]+)\s+\(floor\s+(\d+)\)/m', $data, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $result[$match[1]] = $match[2]; } print_r($result); ?> </pre> Quote Link to comment Share on other sites More sharing options...
JChilds Posted June 12, 2008 Author Share Posted June 12, 2008 Parens are metacharacters that must be escaped if you want to match a literal. <pre> <?php $data = <<<DATA Secretary Office (floor 12) Employee Gym (floor 7) DATA; preg_match_all('/^([^()]+)\s+\(floor\s+(\d+)\)/m', $data, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $result[$match[1]] = $match[2]; } print_r($result); ?> </pre> This will give me all the floor numbers, I only want the secretary floor number. Quote Link to comment Share on other sites More sharing options...
effigy Posted June 13, 2008 Share Posted June 13, 2008 <pre> <?php $data = <<<DATA Secretary Office (floor 12) Employee Gym (floor 7) DATA; preg_match('/^Secretary Office \(floor (\d+)\)/m', $data, $matches); print_r($matches); ?> </pre> 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.