Jump to content

Getting everything before the -


DavidRoberts60

Recommended Posts

if you don't care what characters come before the -, and if they are not always going to be digits, you can use this preg_match

 

$string = "728334-sdfnjsdh";
$pattern = "~(.*)-~";
preg_match($pattern,$string,$matches);
print_r($matches);

 

$matches[1] will hold the value that you want. If you expect the characters that come before the - to always be digits, then you can use something like this

 

$string = "728334-sdfnjsdh";
$pattern = "~(\d*)-~";
preg_match($pattern,$string,$matches);
print_r($matches);

 

where again, $matches[1] will hold your value

if you don't care what characters come before the -, and if they are not always going to be digits, you can use this preg_match

 

$string = "728334-sdfnjsdh";
$pattern = "~(.*)-~";
preg_match($pattern,$string,$matches);
print_r($matches);

 

$matches[1] will hold the value that you want. If you expect the characters that come before the - to always be digits, then you can use something like this

 

$string = "728334-sdfnjsdh";
$pattern = "~(\d*)-~";
preg_match($pattern,$string,$matches);
print_r($matches);

 

where again, $matches[1] will hold your value

 

Thanks for your help.

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.