I have two 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?
做这个的最好方式是什么?
4 个解决方案
#1
2
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()
:
尽管如此,这是使用preg_split()的示例:
$parts = preg_split('/[-\/]/', $string);
#2
1
Jason's right. Here's a way to do it with string functions:
杰森是对的。这是使用字符串函数执行此操作的方法:
$str1 = "email-info";
$result = substr($str1, 0, strlen($str1) - strpos('-'));
#3
0
You can use PHP's split method
您可以使用PHP的split方法
$StringYouWant = split($StringNeedsSplitting, '(-|/)')[0]
#4
0
$name = preg_replace('/[-\/](.*)$/', '', $email);
#1
2
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()
:
尽管如此,这是使用preg_split()的示例:
$parts = preg_split('/[-\/]/', $string);
#2
1
Jason's right. Here's a way to do it with string functions:
杰森是对的。这是使用字符串函数执行此操作的方法:
$str1 = "email-info";
$result = substr($str1, 0, strlen($str1) - strpos('-'));
#3
0
You can use PHP's split method
您可以使用PHP的split方法
$StringYouWant = split($StringNeedsSplitting, '(-|/)')[0]
#4
0
$name = preg_replace('/[-\/](.*)$/', '', $email);