- substr_before
1 Bash[ | ]
Bash
Copy
str='hello world'
echo ${str%%ll*}
echo ${str%%l*}
echo ${str%%x*}
Loading
2 Go[ | ]

Go
Copy
package main
import (
"fmt"
"strings"
)
func substrBefore(haystack string, needle string) string {
pos := strings.Index(haystack, needle)
if pos == -1 {
return ""
}
return haystack[:pos]
}
func main() {
fmt.Println(substrBefore("hello world", "l")) // he
fmt.Println(substrBefore("hello world", "ll")) // he
fmt.Println(substrBefore("hello world", "x")) //
}
Loading
3 PHP[ | ]
PHP
Copy
var_dump( strstr('hello world', 'll', true) );
var_dump( strstr('hello world', 'l', true) );
var_dump( strstr('hello world', 'x', true) );
Loading
PHP
Copy
function substr_before($needle, $haystack) { return strstr($haystack, $needle, true); }
var_dump( substr_before('ll', 'hello world') );
var_dump( substr_before('l', 'hello world') );
var_dump( substr_before('x', 'hello world') ); // bool(false)
Loading