tags:

views:

65

answers:

3

Hi,

I need help with writing regular expression for this pattern in PHP:

[[{"type":"media","view_mode":"small","fid":"1","attributes":{"width":0,"height":0,"src":"http://localhost/x.png"}}]]

This is part of the text and I am trying to replace this by something else.

Would like to use preg_replace_all() but can't figure out what would be the pattern. Any help appreciated.

+1  A: 

This looks like JSON encoded data, which you can parse cleanly with json_decode, and put together again with json_encode. No need for regexes here.

Pekka
Beat me to it. :)
Jimmy Shelter
Nope, though its json I need to identify this pattern passed from the form and then replace it. I need to first identify this from other text.
nutalk
Okay, so what exactly is the pattern?
Pekka
<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":"http://localhost/d7mw/sites/default/files/styles/thumbnail/logo.png"}}]] 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> Lets say this is text.
nutalk
A: 

This is part of the text and I am trying to replace this by something else.

What? What part of the original string are you trying to replace with what other string? Please clarify so I can help.

Mr-sk
Lets say this is 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":"http://localhost/d7mw/sites/default/files/styles/thumbnail/logo.png"}}]] 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>" and I need to identify the pattern above
nutalk
+2  A: 

Since you say you need to identify these JSON-strings inside a normal string, you could use this pattern:

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

meaning:

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

Because of the s flag, the . in the regex will also match line breaks.

Demo:

$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);

which will output:

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

)
Bart Kiers
Thanks, looks like someone understood the question. Will try this :)
nutalk
Note that I only understood it after you posted the fact that this JSON string was inside some other string.
Bart Kiers
I did write "This is part of some text" in the original question. Thanks, Also while you are at it whats the best way to strip out front and end "[[" so that I can use json_decode to convert it into a php object/array.
nutalk
str_replace(']]', '', $str);
Mr-sk
You could use some look arounds: `preg_match_all('/(?<=\[\[).*?(?=]])/s', $text, $matches)`, or group (and then use) only the inner part: `preg_match_all('/\[\[(.*?)]]/s', $text, $matches)`
Bart Kiers
Hi, Can you tell me what would be the regex for the same in Javascript? I tried some variations but String.match() doesnt return stuff.
nutalk