Welcome to WebmasterWorld Guest from 54.226.194.180

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Issue With Preg replace

     

kkonline

1:41 pm on Mar 24, 2014 (gmt 0)

5+ Year Member



Hi,
I have data as below
// [ { "id": "4674509" ,"t" : "RELIANCE" ,"e" : "NSE" ,"l" : "889.40" ,"l_fix" : "889.40" ,"l_cur" : "Rs.889.40" ,"s": "0" ,"ltt":"12:44PM GMT+5:30" ,"lt" : "Mar 22, 12:44PM GMT+5:30" ,"c" : "+3.60" ,"c_fix" : "3.60" ,"cp" : "0.41" ,"cp_fix" : "0.41" ,"ccol" : "chg" ,"pcls_fix" : "885.8" } ] 

in a variable and want to split it with
" ,"


My code doesn't return the individual array values

$grab_data = file_get_contents_curl("kkonline.org/data.php");

$matches = preg_split('" ,"', $grab_data);
echo $matches[0];

Now here $matches[0]; returns the complete data not the splitted values

Any idea what is wrong?

robzilla

1:59 pm on Mar 24, 2014 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



$string = '// [ { "id": "4674509" ,"t" : "RELIANCE" ,"e" : "NSE" ,"l" : "889.40" ,"l_fix" : "889.40" ,"l_cur" : "Rs.889.40" ,"s": "0" ,"ltt":"12:44PM GMT+5:30" ,"lt" : "Mar 22, 12:44PM GMT+5:30" ,"c" : "+3.60" ,"c_fix" : "3.60" ,"cp" : "0.41" ,"cp_fix" : "0.41" ,"ccol" : "chg" ,"pcls_fix" : "885.8" } ]';

$matches = preg_split('" ,"', $string);

echo $matches[0];


This returns a string with the contents: // [ { "id": "4674509"

What does var_dump($matches) tell you? And var_dump($grab_data)?

By the way, since you're not actually using any regular expressions, you're better off using explode() instead of preg_split().

penders

3:22 pm on Mar 24, 2014 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



It looks very JSON-esque, any reason why you're not processing it as JSON?
 

Featured Threads

Hot Threads This Week

Hot Threads This Month