preg_replace 模式替换与单个字符?
preg_replace pattern replacement versus single character?
我正在使用这一行用这个 ^_^
模式替换一个愚蠢的字符串
$newText = trim(preg_replace('/\^_^+/', "\r\n", $newText));
图案可以是这样的,也可以是相同图案的倍数...
^_^ OR ^_^^_^ or ^_^^_^^_^
我不是正则表达式之王,谁能帮我理解如何替换字符串而不是单个字符?
当我想像这样 ^
替换单个字符串或多个相同的字符串时,它会起作用,例如..
$newText = trim(preg_replace('/\^+/', "\r\n", $newText));
我试过这个和其他类似的组合,但没有成功
preg_replace('/\^_\^+/', "\r\n", $newText)
您需要转义两个 ^
并将 \^_\^
括在括号中:
$newText = trim(preg_replace('/(\^_\^)+/', "\r\n", $newText));
我正在使用这一行用这个 ^_^
模式替换一个愚蠢的字符串
$newText = trim(preg_replace('/\^_^+/', "\r\n", $newText));
图案可以是这样的,也可以是相同图案的倍数...
^_^ OR ^_^^_^ or ^_^^_^^_^
我不是正则表达式之王,谁能帮我理解如何替换字符串而不是单个字符?
当我想像这样 ^
替换单个字符串或多个相同的字符串时,它会起作用,例如..
$newText = trim(preg_replace('/\^+/', "\r\n", $newText));
我试过这个和其他类似的组合,但没有成功
preg_replace('/\^_\^+/', "\r\n", $newText)
您需要转义两个 ^
并将 \^_\^
括在括号中:
$newText = trim(preg_replace('/(\^_\^)+/', "\r\n", $newText));