字符串示例。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