字符串示例。php:用字符串中的相同单词匹配特定单词并将其删除
$val = "Olympus Stylus VG-165 mp";
我想从字符串中删除MP
结果:Olyus Stylus VG-165.
从字符串的结尾奥林巴斯删除 'MP' 和也。
如何从末尾移除MP,但也不是来自奥林巴斯
欲望输出继电器:Olympus Stylus VG-165.
字符串示例。php:用字符串中的相同单词匹配特定单词并将其删除
$val = "Olympus Stylus VG-165 mp";
我想从字符串中删除MP
结果:Olyus Stylus VG-165.
从字符串的结尾奥林巴斯删除 'MP' 和也。
如何从末尾移除MP,但也不是来自奥林巴斯
欲望输出继电器:Olympus Stylus VG-165.
使用这个简单的单行
echo substr("Olympus Stylus VG-165 mp",0,22);
$var = preg_replace('# mp$#',' ',$var) ; // Remove if there is " mp" at the end
$var = preg_replace('#^mp #',' ',$var) ; // Remove if "mp " on start
$var = preg_replace('# mp #',' ',$var) ; // Remove if " mp " everywhere
$var = trim($var) ; // Remove the extras space on start and end of the string
的其他方法,使其:
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
如果mp在中心那么。 – 2015-04-02 07:15:40
您可以使用substr并给它一个位置 – 2015-04-02 07:03:23