-2

How do you specify that you want to return a substring containing all characters from the start of a string up to but not including the first dot or dash?

For example if the original string is:

'abcdefg.hij-k'

or if the original string is:

'abcdefg.hi-j.k.l.mn-op'

Then the same substring of:

'abcdefg'

should be returned.

The key thing here is that there may be multiple dots and dashes occurring randomly and we are only interested in the first chunk of characters.

EDIT: A dot or a dash may occur first.

Donal.Lynch.Msc
  • 3,365
  • 12
  • 48
  • 78

2 Answers2

2

You could use preg_split:

$res = preg_split('/[-.]/', $string);

The string you want is in $res[0]

Or preg_match:

preg_match('/^([^-.]+)/', $string, $matches);

The result is in $match[1]

Toto
  • 89,455
  • 62
  • 89
  • 125
-1

Try using the explode function.

$final = explode(".", $originalstr);

first should be $final[0];

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
Peter
  • 1,589
  • 2
  • 20
  • 27