2009-08-18 28 views
2

如何用預浸取代替單個問號。php:preg替換單個問號?

+0

這可能是值得添加一些測試字符串與你想要的替換值是什麼,所以我們可以理解單個問號對你意味着什麼:) – gnarf 2009-08-18 21:12:40

回答

9
preg_replace('/\?/', 'replacement', $original, 1) 
2

如果這是您要更換單個字符,你可能不需要參看preg_解決方案:一個「簡單」的str_replace函數可以做的伎倆還有:

www.php.net/str_replace

0

如果你想確保不更換任何問號串testing ??中,你可以這樣做:

// using negative lookbehind/ahead to ensure that the question mark 
// doesn't have a "friend" 
$new = preg_replace('/(?<!\?)\?(?!\?)/', 'replacement', $original); 

如果您正在尋找只替換第一個問號在S tring - 混沌的答案是你想要的