Sorry for this basic question, but I've been looking over all the info about preg_replace I can find and I still can't figure this out.. I have a large string, like this, for example:
对不起这个基本问题,但我一直在查看有关preg_replace的所有信息,我可以找到,但我仍然无法解决这个问题。我有一个大字符串,例如:
$string= '# tjs { fassdaf } #fsk { fssf} # fskff { casf }';
And when I do this, it replaces the entire pattern, not just the part in ( ) as I expect it to do.. I am wondering how I can just replace the part in ( ).. thanks
当我这样做时,它取代了整个模式,而不是像我期望的那样替换()中的部分。我想知道如何在()中替换部分...谢谢
$pattern= '/#.*tjs.*\{.*(.*)\}/imsU';
$replacement= "test";
$return_string = preg_replace ($string, $pattern, $replacement );
expected replaced string:
预期更换字符串:
'# tjs {test} #fsk { fssf} # fskff { casf }';
1 个解决方案
#1
3
$pattern= '/(#\s*tjs\s*\{\s*)(.*?)(\s*\})/imsU';
$replacement= "test";
$return_string = preg_replace($pattern,'$1'.$replacement.'$3',$string);
#1
3
$pattern= '/(#\s*tjs\s*\{\s*)(.*?)(\s*\})/imsU';
$replacement= "test";
$return_string = preg_replace($pattern,'$1'.$replacement.'$3',$string);