如何根据列表查找单词并取消(删除)其后的字符?
How find words and abolish (delete) characters after it based in a list?
如果我想检查 'php' 是否存在于字符串中并删除它后面的单词,我可以使用:
$string = 'hello world php is a bla bla..';
$string = explode(' php ', $string);
echo $string[0];
但是如果我有多个单词而不是 'php' 怎么办?例如下面几首歌曲名称有 'feats'、'feat'、'ft'、'ft.'.. 就是其中的四个。
我想让 'sia - elastic feat brunos mars' 变成 'sia - elastic'。
你可以使用
$string = 'hello world php is a bla bla..';
$string_before = strstr($string, 'php', true);
这将检查第一次出现的字符串 'php'.. 并在之后删除其余部分。
这是您需要的代码:
<?php
$string = 'sia - elastic feat brunos mars';
$array = array('feats', 'feat', 'ft', 'ft.');
for($i = 0; $i < count($array); $i++) {
$out = explode(' ' . $array[$i] . ' ', $string);
if (count($out) > 1) {
break;
}
}
echo $out[0];
?>
如果我想检查 'php' 是否存在于字符串中并删除它后面的单词,我可以使用:
$string = 'hello world php is a bla bla..';
$string = explode(' php ', $string);
echo $string[0];
但是如果我有多个单词而不是 'php' 怎么办?例如下面几首歌曲名称有 'feats'、'feat'、'ft'、'ft.'.. 就是其中的四个。
我想让 'sia - elastic feat brunos mars' 变成 'sia - elastic'。
你可以使用
$string = 'hello world php is a bla bla..';
$string_before = strstr($string, 'php', true);
这将检查第一次出现的字符串 'php'.. 并在之后删除其余部分。
这是您需要的代码:
<?php
$string = 'sia - elastic feat brunos mars';
$array = array('feats', 'feat', 'ft', 'ft.');
for($i = 0; $i < count($array); $i++) {
$out = explode(' ' . $array[$i] . ' ', $string);
if (count($out) > 1) {
break;
}
}
echo $out[0];
?>