How to remove anything in a string after “-”?

2020-02-16 17:15发布

This is the example of my string.

$x = "John Chio - Guy";
$y = "Kelly Chua - Woman";

I need the pattern for the reg replace.

$pattern = ??
$x = preg_replace($pattern, '', $x); 

Thanks

标签: php regex
7条回答
再贱就再见
2楼-- · 2020-02-16 18:08

Use the strstr function.

Example:

$my_string = "This is my best string - You like it?";
$my_new_string = strstr($my_string, '-', true);

echo($my_new_string);
查看更多
登录 后发表回答