당신은 당신이 (더 중첩는 "테스트 [테스트 [키]]"와 같은 bracked하지가 가정) 찾고있는 무엇을 달성 할 수없는 str_replace 함수에서 빌드를 사용하여 시도 할 수 :
$str = "iwdnowfreedom[body_style][var]";
echo trim(str_replace(array("][", "[", "]"), "_", $str), "_");
또는 당신이 선호하는 경우 정규 표현식 (중첩 된 괄호가이 방법으로 잘 작동) :
$input = "iwdnowfreedom[body_style][var]";
$pattern = '/(\[+\]+|\]+\[+|\[+|\]+)/i';
$replacement = '_';
$fixedOutput = trim(preg_replace($pattern, $replacement, $input), "_");
echo $fixedOutput;
난 당신이 또한 당신이 문자열을 가질 수 있다는 것을 의미 생각과 같은
<input id="blah" name="test[hello]" />
와 이름을 구문 분석 당신은 다만 수 속성 :
function parseNameAttribute($str)
{
$pos = strpos($str, 'name="');
if ($pos !== false)
{
$pos += 6; // move 6 characters forward to remove the 'name="' part
$endPos = strpos($str, '"', $pos); // find the next quote after the name="
if ($endPos !== false)
{
$name = substr($str, $pos, $endPos - $pos); // cut between name=" and the following "
return trim(preg_replace('/(\[+\]+|\]+\[+|\[+|\]+)/i', '_', $name), '_');
}
}
return "";
}
또는
function parseNameAttribute($str)
{
if (preg_match('/name="(.+?)"/', $str, $matches))
{
return trim(preg_replace('/(\[+\]+|\]+\[+|\[+|\]+)/i', '_', $matches[1]), '_');
}
return "";
}
당신은 당신이에서와로 교체하고 싶은의 예를 들어 주실 수 있습니까? – TwiNight
왜 [and]에 str_replace를 사용하지 않는가? 마지막 발생은 무시 될 수 있습니다 ... – RonaldBarzell
샘플 코드는 당신이 텍스트 설명과 관련없는 것 같습니다. 나는 당신의 텍스트 설명에 "이름"을 보지 못했지만, 당신은 그것이 isnyou 코드 패턴을 사용합니다. –