The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Parsing through text
Discuss Parsing through text in the PHP Development forum on Dev Shed. Parsing through text PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 28th, 2012, 09:46 AM
|
|
|
|
Parsing through text
I haven't touched PHP in a long time and needed some help.
I have survey and in the survey once you upload the documents, it stores them as such in the database:
[{ },{ "size":"126.189"size":"126.469","name":"RFC_MarketingReqForm.docx","filename":"fu_378r8fczc3u5nen","ext":"docx"","name":"RFC_ARMCBillpay_081412.docx","filename":"fu_ws6t2urxz2qg39c","ext":"docx" },{ "size":"126.469","name":"RFC_MarketingReqForm.docx","filename":"fu_7ahdzpscp5dh82w","ext":"docx" }]
I need to go through this and pull out the filename and ext to create a url link to the documents on a custom reports page that I am creating.
PHP Code:
$text = $row['response'];
$newtext = preg_split( "/ (;|\"|{|}|,) /", $text );
$newtext = explode(',', $text);
$count = count($newtext);
for ($i = 0; $i < $count; $i++) {
echo $newtext[$i] . "<br />";
}
I was trying to break it apart piece by piece, but I realized I am not really sure how to get the filename and ext out alone. Thank you.
|

December 28th, 2012, 10:11 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
whoever designed this database, slap him and don't ever let him near the code again -- I hope it wasn't you?
Sh*t like this is exactly why people preach normalization, but obviously some "developers" still have never heard of it.
OK, so you have a bunch of crap data which looks like JSON. So what you'll have to do is take a JSON parser (if my guess was right) and read out the pieces you want to fetch -- in the hopes that the string isn't broken.
If you have any chance to repair the database, that's actually the first thing you should do.
Do not fumble with home-made regexes. That just makes everything even crappier.
|

December 28th, 2012, 10:13 AM
|
|
|
|
It is from LimeSurvey, open source survey system.
I kept on reading and came across the same thing that it is JSON. I am trying to get it to work with json_decode, will update my progress if I succeed.
|

December 28th, 2012, 05:54 PM
|
 |
Known to taste like chicken
|
|
Join Date: Aug 2003
Location: In front of my computer
|
|
the data you posted is corrupt. If that is a direct copy/paste from the system then you are in trouble.
I fixed the data up (I had to make up some stuff to fill in the blanks and fix the structure). I ended up with:
Code:
[{ },
{ "size":"126.469","name":"RFC_MarketingReqForm.docx","filename":"fu_378r8fczc3u5nen","ext":"docx" },
{ "size":"126.469","name":"RFC_ARMCBillpay_081412.docx","filename":"fu_ws6t2urxz2qg39c","ext":"docx" },
{ "size":"126.469","name":"RFC_MarketingReqForm.docx","filename":"fu_7ahdzpscp5dh82w","ext":"docx" }]
that let me call json_decode() on it, which output this array:
Code:
Array
(
[0] => Array
(
)
[1] => Array
(
[size] => 126.469
[name] => RFC_MarketingReqForm.docx
[filename] => fu_378r8fczc3u5nen
[ext] => docx
)
[2] => Array
(
[size] => 126.469
[name] => RFC_ARMCBillpay_081412.docx
[filename] => fu_ws6t2urxz2qg39c
[ext] => docx
)
[3] => Array
(
[size] => 126.469
[name] => RFC_MarketingReqForm.docx
[filename] => fu_7ahdzpscp5dh82w
[ext] => docx
)
)
so from that you could get the filename using a loop and:
PHP Code:
$filename = $array[$i]['filename'].".".$array[$i]['ext'];
also bare in mind that json_decode($jsondata,true) will give you an assoc array which you can reference like i did above, while omitting the true will give you an object.
__________________
"Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe
Looking for a project Idea?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|