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
https://forums.phpfreaks.com/topic/90165-preformatted-text-to-variable/
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?

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

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?

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

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.