Php 用preg_Replace替换符号

Php 用preg_Replace替换符号,php,regex,replace,preg-replace,Php,Regex,Replace,Preg Replace,我可以用str_replace替换符号吗 假设我想要 <!-- Hello there --> 我可能对一些人来说很容易,仍然在学习str_replace和preg_replace你可以通过preg_replace来实现这一点 <?php $string = '<!-- Hello there How are you -->'; $pattern = "/^<!-- (\w+).*/i"; $replacement = '[$1]'; echo preg_r

我可以用str_replace替换符号吗

假设我想要

<!-- Hello there -->

我可能对一些人来说很容易,仍然在学习str_replace和preg_replace

你可以通过
preg_replace
来实现这一点

<?php
$string = '<!-- Hello there How are you -->';
$pattern = "/^<!-- (\w+).*/i";
$replacement = '[$1]';
echo preg_replace($pattern, $replacement, $string);
?> //=> [Hello]
/=>[你好]

它捕获一个或多个单词字符,这些字符位于
之后。您可以为str_replace函数传递两个数组。例如:

$find = array('<!-- ', ' there -->');
$replaceWith = array('[', ']');
$string = '<!-- Hello there -->';
$hello = str_replace($find, $replaceWith, $string);
$find=array(“”);
$replaceWith=数组('[',']');
$string='';
$hello=str_replace($find,$replaceWith,$string);

检查一下

回答你的问题:是的,你可以。试着使用这个功能两次。这是你想要的吗?
$find = array('<!-- ', ' there -->');
$replaceWith = array('[', ']');
$string = '<!-- Hello there -->';
$hello = str_replace($find, $replaceWith, $string);