Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php:将特定单词与字符串中的相同单词进行匹配,并将其删除_Php - Fatal编程技术网

php:将特定单词与字符串中的相同单词进行匹配,并将其删除

php:将特定单词与字符串中的相同单词进行匹配,并将其删除,php,Php,字符串示例 $val = "Olympus Stylus VG-165 mp"; 我正在尝试从字符串中删除mp 结果:Olyus触针VG-165。 它从字符串的末尾和olympus中删除“mp” 我怎样才能从末端移除mp,但也不能从奥林巴斯移除mp 欲望输出:Olympus手写笔VG-165。使用这条简单的单线 echo substr("Olympus Stylus VG-165 mp",0,22); 另一种方法是: function testMp($v) { return $v != 'm

字符串示例

$val = "Olympus Stylus VG-165 mp";
我正在尝试从字符串中删除mp

结果:
Olyus触针VG-165。

它从字符串的末尾和olympus中删除“mp”

我怎样才能从末端移除mp,但也不能从奥林巴斯移除mp


欲望输出:
Olympus手写笔VG-165。

使用这条简单的单线

echo substr("Olympus Stylus VG-165 mp",0,22);
另一种方法是:

function testMp($v) { return $v != 'mp' ; } // Function called by array_filter
$words = explode(' ',$var) ; // Explode the words of the string in an array
$words = array_filter($words,'testMp') ; // Test each word on the array and remove if "mp" found (via testMp)
$var = implode(' ',$words) ; // Restore the words array into a simple string

你可以使用substr并给它一个位置,如果mp在中间的话。
function testMp($v) { return $v != 'mp' ; } // Function called by array_filter
$words = explode(' ',$var) ; // Explode the words of the string in an array
$words = array_filter($words,'testMp') ; // Test each word on the array and remove if "mp" found (via testMp)
$var = implode(' ',$words) ; // Restore the words array into a simple string