2010-01-06 5 views

답변

3

당신이 일반 문자열 내에서 이러한 JSON-문자열을 식별하기 위해 필요하다고 때문에, 당신은이 패턴을 사용할 수 있습니다

'/\[\[.*?]]/s' 

의미 : 때문에 s 플래그의

\[\[ # match two consecutive '['-s 
.*?  # reluctantly match any character 
]]  # match two consecutive ']'-s 

. 정규식에서 줄 바꿈도 일치합니다.

데모 :

$text = '<p>blahhah blahaa blahhah blahaablahhah blahaablahhah 
    blahaablahhah blahaablahhah blahaablahhah blahaablahhah 
    blahaablahhah blahaa [[{"type":"media","view_mode":"small", 
    "fid":"1","attributes":{"width":0,"height":0,"src": 
    "localhost/d7mw/sites/…;}}]] more blah more blah more blah 
    more blahmore blah more blahmore blah more blahmore blah 
    more blahmore blah more blahmore blah more blahmore blah 
    more blahmore blah more blahmore blah more blahmore blah 
    more blah</p>'; 
preg_match_all('/\[\[.*?]]/s', $text, $matches); 
print_r($matches); 

출력됩니다

Array 
(
    [0] => Array 
     (
      [0] => [[{"type":"media","view_mode":"small", 
    "fid":"1","attributes":{"width":0,"height":0,"src": 
    "localhost/d7mw/sites/…;}}]] 
     ) 

) 
+0

감사합니다. 누군가가 질문을 이해 한 것 같습니다. 이것을 시도 할 것이다 :) – nutalk

+0

이 JSON 문자열이 다른 문자열 안에 있다는 사실을 게시 한 후에 만 ​​이해할 수 있음에 유의하십시오. –

+0

나는 원래 질문에서 "This is some text"라고 썼다. 고마워, 또한 프런트 엔드와 ""[[ "그래서 나는 그것을 PHP 개체/배열로 변환 json_decode를 사용할 수 있도록 스트립하는 가장 좋은 방법은 뭐죠? – nutalk

2

이 코드는 JSON 인코딩 된 데이터처럼 보입니다.이 코드는 json_decode으로 깨끗하게 구문 분석하고 json_encode과 함께 다시 입력 할 수 있습니다. regexes 필요는 없습니다.

+0

그것에 나를 이길. :) –

+0

아니, 그 json 나는 양식에서 전달 된이 패턴을 식별하고 그것을 대체해야합니다. 먼저 다른 텍스트에서이를 식별해야합니다. – nutalk

+0

좋아요, 패턴이 정확히 무엇입니까? –

관련 문제