如何丢弃PHP中某个字符串后的所有内容?

Phi*_*ton 2 php string

我想在PHP中使用一个字符串并丢弃某个字符后的所有内容.但是,它不仅需要搜索一个字符,还需要搜索它们的数组.一旦它到达数组中的一个字符,它应该在该点之前返回字符串.

例如,如果我有一个数组:

$chars = array("a", "b", "c");
Run Code Online (Sandbox Code Playgroud)

我将如何通过以下字符串...

log dog hat bat
Run Code Online (Sandbox Code Playgroud)

......最后得到:

log dog h
Run Code Online (Sandbox Code Playgroud)

任何解决方案都将非常感谢.:)

Vin*_*vic 9

strcspn功能是你在找什么.

<?php

$mask = "abc";

$string = "log dog hat bat";

$result = substr($string,0,strcspn($string,$mask));

var_dump($result);

?>
Run Code Online (Sandbox Code Playgroud)