PHP - Get all characters before an array of characters
I have tw开发者_运维技巧o strings:
email-info
and info/email
I need to retrieve all of the characters before -
or /
appears, so that, after being formatted, the strings look like this:
email
and info
What is the best way to do this?
This is pretty trivial for regular expressions. That is to say you could use something else (i.e. string functions).
Nonetheless, here's an example using preg_split()
:
$parts = preg_split('/[-\/]/', $string);
Jason's right. Here's a way to do it with string functions:
$str1 = "email-info";
$result = substr($str1, 0, strlen($str1) - strpos('-'));
You can use PHP's split method
$StringYouWant = split($StringNeedsSplitting, '(-|/)')[0]
$name = preg_replace('/[-\/](.*)$/', '', $email);
精彩评论