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