darkfreaks Posted February 25, 2012 Share Posted February 25, 2012 i want to modify my search thing using file_get_contents to match the profile age instead of suspension reason. in order to change this i need to match this. line. <b>Age:</b> 16 i am trying to do something like case($line=="<b>Age:</b>") then inside of that put an age variable but i get nothing. for age i did $age= ($age < 18) ? 'minor' : 'adult'; but nothing works when i pass age into the line string ultimately i want to make it so that if the matched age is less than 18 to say minor else pass as an adult. Quote Link to comment https://forums.phpfreaks.com/topic/257750-using-regex-to-match-age/ Share on other sites More sharing options...
abareplace Posted February 25, 2012 Share Posted February 25, 2012 This is probably what you want: <?php $string = '<b>Age:</b> 16'; if (preg_match('~<b>Age:</b> (\\d+)~', $string, $match)) { echo ((int)$match[1] < 18) ? 'minor' : 'adult'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257750-using-regex-to-match-age/#findComment-1321072 Share on other sites More sharing options...
AyKay47 Posted February 25, 2012 Share Posted February 25, 2012 This is probably what you want: <?php $string = '<b>Age:</b> 16'; if (preg_match('~<b>Age:</b> (\\d+)~', $string, $match)) { echo ((int)$match[1] < 18) ? 'minor' : 'adult'; } ?> no. If you are not sure of the age data and you need a tight regex you can use this. $string = '<b>Age:</b> 16'; if (preg_match('~^<b>Age:</b> ([1-9][0-9]?|10[0-5])$~', $string, $match)) { //max age 105 echo ((int)$match[1] < 18) ? 'minor' : 'adult'; } else, if you are sure of the data, you can use this: $string = '<b>Age:</b> 16'; if (preg_match('~^<b>Age:</b> (\d{1,3})$~', $string, $match)) { echo ((int)$match[1] < 18) ? 'minor' : 'adult'; } or even: $pattern = '^<b>Age:</b> (\d+)$'; curious, where are you getting the contents of $line? Quote Link to comment https://forums.phpfreaks.com/topic/257750-using-regex-to-match-age/#findComment-1321133 Share on other sites More sharing options...
darkfreaks Posted February 25, 2012 Author Share Posted February 25, 2012 yeah it is all messed up now. <?php $submit = isset($_POST['submit'])?$_POST['submit']:false; if ($submit) { $url = 'http://vampirefreaks.com/profile.php?user='; $user = $_POST['user']; $file = file($url.$user);//retrieves url and puts into plain text format foreach ($file as $lines) { $line=strip_tags($lines); $line=trim($line); if ($line != ''||!empty($line)) { //echo "$line<br>\n"; // debug code $line = str_replace('LOTCC.bcp();', ' ',$line);//strip out multiply.com javascript code $line = preg_replace('/\s+/', ' ', $line); //strip out white space $line = preg_replace('/\s+$/', ' ', $line);//strip out white space $redopen="<font color=red>"; $greenopen="<font color=green>"; $pinkopen="<font color=#f600ff>"; $blueopen="<font color=blue>"; $purpleopen="<font color=purple>"; $close="</font>"; switch($line){ case ($line =='THIS USER DOES NOT EXIST'||$line == 'Error Retreiving user info'): $message ="$redopen this user is dead!$close"; break; case (preg_match('~^<b>Age:</b> ([1-9][0-9]?|10[0-5])$~', $line, $match)): $message= ((int)$match[1] < 18) ? ''.$redopen.'minor Posting in 18+ Classifieds'.$close.'' : $match[1]; break; }}}} if($message){echo $message;}else{echo $form;} Quote Link to comment https://forums.phpfreaks.com/topic/257750-using-regex-to-match-age/#findComment-1321252 Share on other sites More sharing options...
AyKay47 Posted February 25, 2012 Share Posted February 25, 2012 I think it would be best if you glanced at the switch statement syntax: http://php.net/manual/en/control-structures.switch.php Quote Link to comment https://forums.phpfreaks.com/topic/257750-using-regex-to-match-age/#findComment-1321254 Share on other sites More sharing options...
darkfreaks Posted February 25, 2012 Author Share Posted February 25, 2012 yeah i think i will just remove the switch and use ternaries. Quote Link to comment https://forums.phpfreaks.com/topic/257750-using-regex-to-match-age/#findComment-1321272 Share on other sites More sharing options...
AyKay47 Posted February 25, 2012 Share Posted February 25, 2012 yeah i think i will just remove the switch and use ternaries. Let us know when this has been solved. Quote Link to comment https://forums.phpfreaks.com/topic/257750-using-regex-to-match-age/#findComment-1321276 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.