Jump to content

Preformatted Text to Variable


Helminthophobe

Recommended Posts

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
?>

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.