php 如何先替换multi |最后 |到字符串中的其他字符
php how to replace multi first | and last | to other character in string
例子我有:Today is | nice day | go to | school now | and enjoy | your life |.
我试过了:
$text = str_replace('| ','"',$text);
$text = str_replace(' |','"',$text);
结果是:
Today is "nice day "go to "school now "and enjoy "your life ".
你可以看到结果不好 -> ...day "go
; ...now "and
; ...life ".
结果应该是:Today is "nice day" go to "school now" and enjoy "your life".
我有很多这样的文字要替换,请帮忙!
试试这个,使用正则表达式替换匹配项。在这种情况下,我们还捕获两个 | 之间的文本。标记并在替换中使用捕获的文本:
$text = "Today is | nice day | go to | school now | and enjoy | your life |.";
echo $text; echo "\r\n\r\n";
$text = preg_replace("~\| (.*?) \|~", '""', $text);
echo $text; echo "\r\n\r\n";
看到这个fiddle:
例子我有:Today is | nice day | go to | school now | and enjoy | your life |.
我试过了:
$text = str_replace('| ','"',$text);
$text = str_replace(' |','"',$text);
结果是:
Today is "nice day "go to "school now "and enjoy "your life ".
你可以看到结果不好 -> ...day "go
; ...now "and
; ...life ".
结果应该是:Today is "nice day" go to "school now" and enjoy "your life".
我有很多这样的文字要替换,请帮忙!
试试这个,使用正则表达式替换匹配项。在这种情况下,我们还捕获两个 | 之间的文本。标记并在替换中使用捕获的文本:
$text = "Today is | nice day | go to | school now | and enjoy | your life |.";
echo $text; echo "\r\n\r\n";
$text = preg_replace("~\| (.*?) \|~", '""', $text);
echo $text; echo "\r\n\r\n";
看到这个fiddle: