Jump to content

Breaking up a string


jamiegee

Recommended Posts

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

Link to comment
Share on other sites

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

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".

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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().

Link to comment
Share on other sites

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

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.