. 당신이 무엇을 설명 할 수있는 가장 간단한 방법은 같은 것을 할 수 있습니다 :
$output = preg_replace('/.*\("br"\).*/', '<span class="br"></span>', $input);
을하지만 그건 당신이 실제로 원하는 있는지 모르겠어요. 그러면 처음 문자열의 모든 텍스트가 제거되고 <span class="br"></span>
블록으로 바뀝니다. 남은 문자는 모두 <span class="br"></span>
입니다.
foo<span class="bar"></span>baz
같은 블록으로 foo("bar")baz
처럼 보이는 블록을 변경하는 것이 좋다고 나에게 들립니다.
$output = preg_replace('/\("(.*?)"\).*/', '<span class="$1"></span>', $input);
그러나, 내가 질문을 읽어가는 길에 단지 내 추측이있다 : 그런 경우에, 당신은 아마 이런 식으로 뭔가를 할 것입니다. 문제를 실제로 해결하려면 , post_string
및 br
이 무엇을 나타내는 지, 그리고 어떻게 다를 수 있는지에 대해 조금 더 알아야합니다. 일부 샘플 입력 및 출력 텍스트가 도움이 될 수 있습니다.
편집 : 최근 편집 결과가 좀 더 명확 해졌습니다. 정규 표현식으로 JavaScript 나 다른 프로그래밍 언어를 구문 분석하려는 것 같습니다. 일반적으로 limitations of regex로 인해 완벽하게 수행 할 수 없습니다. 그러나, 다음은 대부분의 경우에 작동합니다 :
$pattern = '/(["\'])\s*\+\s*\w+\((["\'])(.*?)\2\)\s*\+\s*\1/'
$output = preg_replace($pattern, '<span class="$3"></span>', $input);
설명 :
/
(["\']) #Either " or '. This is captured in backreference 1 so that it can be matched later.
\s*\+\s* #A literal + symbol surrounded by any amount of whitespace.
\w+ #At least one word character (alphanumeric or _). This is "figure" in your example.
\( #A literal (character.
(["\']) #Either " or '. This is captured in backreference 2.
(.*?) #Any number of characters, but the `?` makes it lazy so it won't match all the way to the last `") + "` in the document.
\2 #Backreference 2. This matches the " or ' from earlier. I didn't use ["\'] again because I didn't want something like 'blah" to match.
\) #A literal) character.
\s*\+\s* #A literal + symbol surrounded by any amount of whitespace.
\1 #Backreference 1, to match the first " or ' quote in the string.
/
희망 비교적 이해하기 쉽다. 정규식 패턴이 무엇을하는지 설명하기가 어려울 수 있습니다. 그래도 이것이 여전히 어려울 경우 유감입니다. 그래도 혼란 스러우면 backreferences 및 lazy quantifiers에 대한 추가 정보가 있습니다.
역 참조 구문에 대해 잘 모르겠습니다. 요즘 PHP에서는 보통 코드를 작성하지 않습니다. 누구든지 나를 바로 잡고 싶어한다면 나는 그것을 환영한다.난 당신이 몇 가지를 변환하는 가정
// " + figure("br") + "
$pre = '" + figure';
$post = ' + "';
// escape
$pre = preg_quote($pre, "#");
$post = preg_quote($post, "#");
// then the regex becomes easy
$string = preg_replace(
"#$pre\(\"(\w+)\"\)$post#",
'<em class="$1"></em>',
$string
);
: 당신이 변수 전후 문자열 (또는 귀하의 경우와 같은 메타 문자 하나)이있는 경우
실제 예제 문자열과 예상되는 출력을 제공하십시오. – mario
@mario 실제 정보 추가 – morgar