Get data from a string
#1
Posted 04 October 2008 - 10:38 AM
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
Posted 04 October 2008 - 08:51 PM
<?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
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
Posted 13 October 2008 - 09:23 AM
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
Posted 13 October 2008 - 01:29 PM
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
Posted 13 October 2008 - 04:55 PM
#6
Posted 13 October 2008 - 10:07 PM
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
Posted 13 October 2008 - 10:13 PM
Brandon W said:
That's interesting. Most people choose to use explode rather than using regular expressions.
#8
Posted 13 October 2008 - 11:05 PM
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
Posted 14 October 2008 - 09:01 PM
Brandon W said:
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
Posted 14 October 2008 - 09:41 PM
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!


Sign In
Create Account

Back to top










