sdhunter Posted May 20, 2009 Share Posted May 20, 2009 I have a string that arrives as "data1-some text here" "data2-some other text here" "data3-some more text here" I would like to extract the "data1" into one variable then the text after the "-" into another variable. I've been looking at ways to do it with str_replace and trim, but can't work it out. Only been coding php a couple of months on and off and its the first time i need to do this. Any help would be much appreciated. TIA Link to comment https://forums.phpfreaks.com/topic/158971-extracting-certain-text-from-string-into-2-variables/ Share on other sites More sharing options...
thebadbad Posted May 20, 2009 Share Posted May 20, 2009 If the hyphen only appears once, you can explode() the string on the hyphen, and get an array with the two parts returned: <?php $str = 'data1-some text here'; $parts = explode('-', $str); //$parts[0] contains 'data1', $parts[1] contains 'some text here' ?> Link to comment https://forums.phpfreaks.com/topic/158971-extracting-certain-text-from-string-into-2-variables/#findComment-838425 Share on other sites More sharing options...
sdhunter Posted May 20, 2009 Author Share Posted May 20, 2009 Thank you very much. That works perfect! Link to comment https://forums.phpfreaks.com/topic/158971-extracting-certain-text-from-string-into-2-variables/#findComment-838450 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.