homepage Welcome to WebmasterWorld Guest from 54.205.105.23
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Issue With Preg replace
kkonline

5+ Year Member



 
Msg#: 4656687 posted 1:41 pm on Mar 24, 2014 (gmt 0)

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

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4656687 posted 1:59 pm on Mar 24, 2014 (gmt 0)

$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

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



 
Msg#: 4656687 posted 3:22 pm on Mar 24, 2014 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved