Jump to content

Get data from a string

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
larrywcc

larrywcc

    Newbie

  • Members
  • Pip
  • 9 posts
OK, I have listed below of example that I need to split out some data...

There are different formats but they need to be entered into the correct string. The way I tried to do this take way to long and then it breaks.

ANY HELP ?? THANKS !!!

Format is as follows as example.

Night Audit - Hyatt - Boston, MA

Night Warehouse Highpick Selector - US Foodservice - Clearfield, UT

Night Cleaner - CO

Night Time Cleaner - USCLEANER, NY

Now all I need from this site is the last State, Town and then the rest of the data

$a1 = Ma
$a2 = Boston
$a3 = Hyatt
$a4 = Night Audit

$a1 = UT
$a2 = Clearfield
$a3 = US Foodservice
$a4 = Night Warehouse Highpick Selector

$a1 = CO
$a2 =
$a3 =
$a4 = Night Cleaner

$a1 = NY
$a2 =
$a3 = USCLEANER
$a4 = Day time cleaner needed

#2
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Here is the code for it mate.


<?php

$string = "Night Warehouse Highpick Selector - US Foodservice - Clearfield, UT";

$split = preg_split('/ - /', $string, -1, PREG_SPLIT_NO_EMPTY);

$section = $split[2];

echo "<pre>";

print_r ($split);

echo "</pre>";

echo "<br />";

$place = preg_split('/,/', $section, -1, PREG_SPLIT_NO_EMPTY);

echo "<pre>";

print_r ($place);

echo "</pre>";

$otherInfo = $split[0];

$otherInfo2 = $split[1];

$town = $place[0];

$state = $place[1];

echo "$otherInfo <br />";

echo "$otherInfo2 <br />";

echo "$town <br />";

echo "$state <br />";

?>


This is for the second one. You just need to store each string above in a variable and then change the name of the variables being used.

I will write a description of this code for you. Well add comments now, but there is the code if you don't want a description.

COMMENTED VERSION

<?php

$string = "Night Warehouse Highpick Selector - US Foodservice - Clearfield, UT";		//Store each string in a variable to be used

$split = preg_split('/ - /', $string, -1, PREG_SPLIT_NO_EMPTY);					//Split the variable $string anywhere with a "-", the -1 just means the entire string. This is now stored in an arry called $split

$section = $split[2];										//Now store the 3rd item, the state and town, into a variable called $section, I know this because I echoed the array below this line

echo "<pre>";

print_r ($split);										//The pre tags just make it easier to read the array, print the array

echo "</pre>";

echo "<br />";

$place = preg_split('/,/', $section, -1, PREG_SPLIT_NO_EMPTY);					//Since we stored the state and town into a variable $section, we need to split that anywhere with a ",".

echo "<pre>";

print_r ($place);										//Print the array to show the town and state have been split.

echo "</pre>";

$otherInfo = $split[0];										//Using the printed arrays above we can now store the correct information into their own variable instead of referring them to as their array item each time.

$otherInfo2 = $split[1];

$town = $place[0];

$state = $place[1];										

echo "$otherInfo <br />";

echo "$otherInfo2 <br />";									//Now echo these arrays to show your final result.

echo "$town <br />";

echo "$state <br />";

?>


See the attached file.

Attached Files


jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#3
larrywcc

larrywcc

    Newbie

  • Members
  • Pip
  • 9 posts
Thanks, That worked GREAT.

Now with the code you sent works GREAT and now on a few data strings I get does this

When I pull the rss string from a different site and look at the code, on the site or though a rss reader it will show the - in some browsers when I pull the rss feed or other browsers it will show ? or — in the source code. now this is different then the - how do I get around this as I tried ? and putting — instead of the - but no luck..

Thanks for your HELP

#4
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Try the escape key. If it is in quotation marks use this key before it "\".
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
As an alternative to preg_split, you might consider using the explode() function.

#6
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Yer I was thinking of using the explode function, but I have never used it before so I did preg_split.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#7
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

Brandon W said:

Yer I was thinking of using the explode function, but I have never used it before so I did preg_split.

That's interesting. Most people choose to use explode rather than using regular expressions.

#8
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
OK, well when I get some time, hopefully after dinner, I will re-write it using the explode() function. I will need to read about it first.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#9
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

Brandon W said:

OK, well when I get some time, hopefully after dinner, I will re-write it using the explode() function. I will need to read about it first.

It's rather simple. Rather than:
$split = preg_split('/ - /', $string, -1, PREG_SPLIT_NO_EMPTY);                    //Split the variable $string anywhere with a "-", the -1 just means the entire string. This is now stored in an arry called $split

$section = $split[2];   

It's just:
$split = explode("-", $string);

$section = $split[2];   


#10
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Hmmm, OK. Well I got an idea to help with his :)
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!