Jump to content

[SOLVED] Extract from String


saf

Recommended Posts

I have a string such as this "something anotherting athirdthing sunrise 7:30 morestuff sunset 6:15 a bunch of other stuff" and I want to pull out specific information in array format (e.g. $Array[sunrise]=>7:30 , $Array[sunset]=>6:15). One of the problems that I am having is that the time changes from day to day.

 

Can anyone give me a function or someting that can solve that?

Link to comment
https://forums.phpfreaks.com/topic/37674-solved-extract-from-string/
Share on other sites

Well, based upon what you have stated you could probably do something with regular expressions. But, creating a RegEx for that is a little comlpex for me. Instead I would search for the position of "sunrise" and then get the content that starts 8 characters after that till the next space. I'd use the same logic for sunset:

 

$startPos = strpos($text,"sunrise")+8;
$endPos = strpos($text," ", $startPos) - $startPos;
$Array[sunrise] = substr($text, $startPos, $endPos);

$startPos = strpos($text,"sunset")+7;
$endPos = strpos($text," ", $startPos) - $startPos;
$Array[sunset] = substr($text, $startPos, $endPos);

Well, based upon what you have stated you could probably do something with regular expressions. But, creating a RegEx for that is a little comlpex for me. Instead I would search for the position of "sunrise" and then get the content that starts 8 characters after that till the next space. I'd use the same logic for sunset:

 

$startPos = strpos($text,"sunrise")+8;
$endPos = strpos($text," ", $startPos) - $startPos;
$Array[sunrise] = substr($text, $startPos, $endPos);

$startPos = strpos($text,"sunset")+7;
$endPos = strpos($text," ", $startPos) - $startPos;
$Array[sunset] = substr($text, $startPos, $endPos);

 

Thanks this worked with slight modifications...

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.