I could use some help writing a regular expression for this dictionary string (I don't use them all that often).
This is an example of the string dictionary:
O:8:"stdClass":5:{s:4:"sent";i:0;s:6:"graded";i:0;s:5:"score";i:0;s:6:"answer";s:14:"<p>Johnson</p>";s:8:"response";s:0:"";}
I want to extract Johnson from the string dictionary.
Any help would be appreciated, thanks.
This is a PHP serialized object. Don't use a regular expression. unserialize()
the data and display the answer
property accordingly.
unserialize($data);
echo $data->answer;
$str = 'O:8:"stdClass":5:{s:4:"sent";i:0;s:6:"graded";i:0;s:5:"score";i:0;s:6:"answer";s:14:"<p>Johnson</p>";s:8:"response";s:0:"";}';
$obj = unserialize($str);
echo $obj->answer;
This would be the correct answer, no regex needed. You may need some additional HTML parsing if you'd want the <p>
tags removed. If the format will always remain the same (and only then!) simply remove the <p>
and </p>
tags.
It looks like you should be using unserialize()
instead and then you can use preg_match to remove the <p>
tags.
$obj = (unserialize('O:8:"stdClass":5:{s:4:"sent";i:0;s:6:"graded";i:0;s:5:"score";i:0;s:6:"answer";s:14:"<p>Johnson</p>";s:8:"response";s:0:"";}'));
preg_match('~<p>([^<]*)</p>~', $obj->answer, $ans);
print_r($ans[1]); //prints Johnson
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.