If you still have access to the complete serialized strings, you could also use PHP to transform to a portable intermediate format like JSON, which R can parse. Then you won't have to take care of all the complex escaping rules in those strings. For example:
library(tidyverse)
unserialize_php <- function(information) {
system2("php", c("-r", shQuote("$output = []; while(($line=fgets(STDIN))!==false) {$new_output = unserialize($line); $output[] = $new_output ? $new_output : (object)[];} echo(json_encode($output));")), stdout = TRUE, stderr = "", input = information) %>% fromJSON(simplifyDataFrame = TRUE)
}
df <- tibble(
ID = 1:3,
information = unserialize_php(
c("a:2:{s:4:\"name\";s:3:\"max\";s:3:\"age\";s:1:\"8\";};",
"a:2:{s:4:\"name\";s:5:\"peter\";s:3:\"age\";s:2:\"10\";};",
"a:2:{s:4:\"name\";s:4:\"susy\";s:3:\"age\";s:3:\"100\";};")
)
)
df
# # A tibble: 3 × 2
# ID information$name $age
# <int> <chr> <chr>
# 1 1 max 8
# 2 2 peter 10
# 3 3 susy 100
The critical piece here is the unserialize_php
function. It forwards all values of the argument information
(or, the information
column of the data frame df
, respectively) as separate lines to a temporary PHP instance. The script to be run in PHP is supplied as a command-line argument (-r
). The script then takes the input line by line, unserializes each line, adds them to a temporary array($output
) and finally outputs the array as JSON. PHP writes to stdout and R picks it up and returns it as the return value of system2
(because stdin = TRUE
). fromJSON
from the jsonlite
package parses the JSON data and transforms it to a data frame. If the string representations passed to the function are malformed, you will see PHP errors in the R console and the respective lines will be empty / NAs.
Note: You need the complete serialized strings, as originally produced by PHP. I edited your original values to what those should look like. Also, this approach requires that R can call PHP via command line on your machine.