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
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);

Link to comment
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);

 

Thanks this worked with slight modifications...

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.