jamiegee Posted June 6, 2013 Share Posted June 6, 2013 Hi I have just got a string from a PDF which you can see below Age Smith Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Joe Main Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Zoe Smith Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Megan Smith Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Damon Rodgers Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Sean Murray Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No No No No No No No No No No No No No No No No Tom Greenwood Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Hollie Michie Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Jordan Farrell Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Jasmine Roll Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes mikel Yussuff Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Matt Clarke Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No No No No No No No No No No No No No No No No Javan Jackson Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Adele Barham Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Greg McPherson Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Toni Salter Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Louise Smith Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Jack Lenton Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes fran Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No roxy Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Matt Anderson Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Craig Hill Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Rob Pearson Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Jade Twigg Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Kamal Alaykan No No No No No No No No No No No No No No No Yes No No No No No No No No No No No No No No No No Aiden Harrison Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Gemma Marciano Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Fran Moy Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Roxanne McDade Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Abbi Moore Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Jade Warne Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes robert Parks Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Joe Clark Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Russell Rodriguez Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Tom Stevenson No No No No No No No No No No No No No No No Yes Chris Moore Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Bea Wyatt Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Gavin Seamer Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No teejay Carnahan Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No No No No No No No No No No No No No No No No No Harry Dickason No No No No No No No No No No No No No No No Yes Matty Downing Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Lucca Harrison Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No Chris Brown Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Liam Barnett No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No As you will see there is a persons name and then 16 Yes/No then onto the next persons name, what i am looking to do is put each set of them into table data so i can then format the data into a html table. I have been trying many different methods and search the whole of Google a dozen times but just cant seem to find a method to do this. Is this even possible? Thanks in advance Jamie Quote Link to comment Share on other sites More sharing options...
teynon Posted June 6, 2013 Share Posted June 6, 2013 Look at doing a preg_match_all (http://php.net/manual/en/function.preg-match-all.php) with pattern (\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+) preg_match_all("/(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)/", $text_to_scan, $results); echo "<pre>" . print_r($results, true) . "</pre>"; Quote Link to comment Share on other sites More sharing options...
Barand Posted June 6, 2013 Share Posted June 6, 2013 (edited) Sean Murray has 32 yes/no values so there is not a consistent pattern edit: Also fran and roxy have single-word names Edited June 6, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
requinix Posted June 6, 2013 Share Posted June 6, 2013 Another option is explode() and array_chunk(). $words = explode(" ", $text_to_scan); $records = array_chunk($words, 18); Quote Link to comment Share on other sites More sharing options...
DavidAM Posted June 6, 2013 Share Posted June 6, 2013 Is this output from an echo() call? Try using var_dump to see if there are actual delimiters in there (TAB, CRLF, etc), that you can explode on or match -- use explode rather than preg_ unless you need complex matching. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 6, 2013 Share Posted June 6, 2013 IF the number of fields is consistent (which your sample data contradicts) then I completely agree with requinix: use explode() and array_chunk(). If however, the data is not consistent, then you can still use explode and then loop through the values. Then just detect when a value is not a "yes" or "no". Quote Link to comment Share on other sites More sharing options...
Christian F. Posted June 6, 2013 Share Posted June 6, 2013 (edited) If there are no line breaks, or other markers that you can split on, this RegExp should do it. With a variable number of yes/no fields and any kind of name: // Split the lines after a "name - answers" pattern, where the answers can be one or many "yes" or "no"s. $splitGroups = "/(.*?) ((?:Yes|No\\s?)+)/"; preg_match_all ($string, $splitGroups, $groups); // Loops through all of the lines found in the text blob. foreach ($groups as $line) { $name = $line[1]; $answers = explode (" ", $line[2]); // Do whatever you need to do with the two variables above here. } Edited June 6, 2013 by Christian F. Quote Link to comment Share on other sites More sharing options...
jamiegee Posted June 7, 2013 Author Share Posted June 7, 2013 Hi Christian F Your solutions looks perfect however i just put that into my project but when i try to echo or print our the $name and $answers i get nothing? Any Ideas? Thanks Jamie Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 7, 2013 Share Posted June 7, 2013 I'll bet you A. Don't have error reporting turned on. and B. Just copy and pasted that, without thinking about what $string needs to be. Quote Link to comment Share on other sites More sharing options...
jamiegee Posted June 7, 2013 Author Share Posted June 7, 2013 haha jessica I am quite new at php but now that new, i replaced string with my variable name as that goes without saying and if you look at the code closely i even noticed that the first 2 attributes of preg_match_all where round the wrong way and fixed accordingly not sure if you picked that up? and just to be precise error reporting is turned on Thanks for your input Quote Link to comment Share on other sites More sharing options...
DavidAM Posted June 7, 2013 Share Posted June 7, 2013 At the risk of repeating myself ... Is this output from an echo() call? Did you check the string carefully to see if it contained whitespace other than 0x20 (space character)? While the \s after "No" accounts for whitespace there, if there is a tab, newline, or even non-breaking space (0xA0) between the name and answers, I that will not be caught (and I'm not sure that "\s" will catch non-breaking spaces). They could even be nulls, though I'm not sure how the browser would render that. A hex dump of the string would help determine this, or var_dump(), or maybe htmlentities(). Quote Link to comment Share on other sites More sharing options...
jamiegee Posted June 7, 2013 Author Share Posted June 7, 2013 HI David thanks for you reply, its from a pdf to text class thats then run through a str_replace to get rid of a load of other stuff which is then put into a variable with the content what you see at the top, i just run this through a var_dump and htmlentities and all looks to be ok, theres all spaces no other characters. When i echo or print the array all i get is array or array ( ) Thanks Jamie Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted June 7, 2013 Share Posted June 7, 2013 print_r($array); Quote Link to comment Share on other sites More sharing options...
jamiegee Posted June 8, 2013 Author Share Posted June 8, 2013 unfortunately that was the first thing i tried :/ 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.