Jump to content

Extracting certain text from string into 2 variables.


sdhunter

Recommended Posts

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

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'
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.