Helminthophobe Posted February 9, 2008 Share Posted February 9, 2008 I'm trying to build a script that takes preformatted text provided by a user, pulls certain things from the text, and then saves it in a variable so I can put it in my database. I know how to put data in my database but I can not figure out how to actually look for and pull the data. Here is a simple example using some text from the forum: Hello Helminthophobe February 08, 2008, 10:15:25 PM * Show unread posts since last visit. Show new replies to your posts. Return To Main Site Total time logged in: 4 hours and 19 minutes. How would I make a script that looks for the "username" and the time "logged in" as well as save the information as a variable so I can display or save the data? Can someone point me in the right direction? Any help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/90165-preformatted-text-to-variable/ Share on other sites More sharing options...
pwes24 Posted February 9, 2008 Share Posted February 9, 2008 You get user name when they login. At this time you can assign the username to a superglobal variable($_SESSION[]) which can in turn be accessed by other pages. Quote Link to comment https://forums.phpfreaks.com/topic/90165-preformatted-text-to-variable/#findComment-462355 Share on other sites More sharing options...
Helminthophobe Posted February 9, 2008 Author Share Posted February 9, 2008 I just used "username" and "logged in" as an example. The script will use text from another site. The user will do a select all and copy from another site. Then they will paste it into my script where it will be read. I don't have control over any of the information provided, but since it will be preformatted and follow a specific layout, I thought there may be a way to pull data from it. Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/90165-preformatted-text-to-variable/#findComment-462358 Share on other sites More sharing options...
Helminthophobe Posted February 10, 2008 Author Share Posted February 10, 2008 Still looking for some guidance if anyone has the time to offer it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/90165-preformatted-text-to-variable/#findComment-463006 Share on other sites More sharing options...
rhodesa Posted February 10, 2008 Share Posted February 10, 2008 RegEx is your best friend in this case. <?php $text = "Hello Helminthophobe February 08, 2008, 10:15:25 PM * Show unread posts since last visit. Show new replies to your posts. Return To Main Site Total time logged in: 4 hours and 19 minutes."; $matches = array(); if(preg_match("/Hello (\w+)\s+(.+)\s+\*/",$text,$matches)){ $username = $matches[1]; $date = $matches[2]; print "User $username was last logged in at $date"; } //Output: //User Helminthophobe was last logged in at February 08, 2008, 10:15:25 PM ?> Quote Link to comment https://forums.phpfreaks.com/topic/90165-preformatted-text-to-variable/#findComment-463014 Share on other sites More sharing options...
Helminthophobe Posted February 10, 2008 Author Share Posted February 10, 2008 Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/90165-preformatted-text-to-variable/#findComment-463108 Share on other sites More sharing options...
Helminthophobe Posted February 10, 2008 Author Share Posted February 10, 2008 After playing with the code you provided and searching through 4 other tutorials on regexp, I still can't figure out how to search for stuff in multiple lines. I was able to modify the code you provided to work in my real application but I'm still struggling. Using the same example, how would I find "Helminthophobe" and "4 hours and 19 minutes"? Basically, how do I search for stuff on multiple lines? Quote Link to comment https://forums.phpfreaks.com/topic/90165-preformatted-text-to-variable/#findComment-463146 Share on other sites More sharing options...
rhodesa Posted February 10, 2008 Share Posted February 10, 2008 This would work. For more help on RegEx though, I would start a topic over in the RegEx part of the forum. <?php $text = "Hello Helminthophobe February 08, 2008, 10:15:25 PM * Show unread posts since last visit. Show new replies to your posts. Return To Main Site Total time logged in: 4 hours and 19 minutes."; $matches = array(); if(preg_match("/Hello (\w+)/",$text,$matches)){ $username = $matches[1]; } $matches = array(); if(preg_match("/Total time logged in:\s*(.+?)\./",$text,$matches)){ $time_logged = $matches[1]; } print "User $username has been logged in for $time_logged"; //Output: //User Helminthophobe has been logged in for 4 hours and 19 minutes ?> Quote Link to comment https://forums.phpfreaks.com/topic/90165-preformatted-text-to-variable/#findComment-463529 Share on other sites More sharing options...
resago Posted February 10, 2008 Share Posted February 10, 2008 for multiple lines just use the s modifier '/regex stuff/s' Quote Link to comment https://forums.phpfreaks.com/topic/90165-preformatted-text-to-variable/#findComment-463530 Share on other sites More sharing options...
Helminthophobe Posted February 11, 2008 Author Share Posted February 11, 2008 Thanks. I ended up do exactly that, rhodesa, which worked good for what I am making. I appreciate the help. I didn't even think to look for a RegEx area. I'll check it out. Thanks again all. Edit: Is the "Solved" option gone or am I just looking in the wrong place? Quote Link to comment https://forums.phpfreaks.com/topic/90165-preformatted-text-to-variable/#findComment-463585 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.