Saphod Posted April 30, 2008 Share Posted April 30, 2008 Hi, here is my problem (very detailed, but the main problem is a regex one, believe me): Viewat.org provides the possibility to embed panorama images in your blog. Unfortunately, this is based on a javascript-code which - in WordPress - is a little tricky to insert into a post. It is possible to embed it via flash directly (as the script does nothing else), but it is done via <object> and <embed> which is not XHTML-valid. For WordPress, a plugin for embedding flash movies XHTML-vaild already exists. This one is used via a pseudo-tag which is translated "on-the-fly" when the post is being shown in the browser (via WordPress filter). Therefore, I would like to write a little plugin with PHP that does the following as soon as a post is saved/published: a) Find the whole string in the post content that has the command for my plugin. Desciption: The command string I am searching for should e.g. look like this: [embed_viewat width=600 height=400 id=1234 lang=de] "width", "height" and "lang" should be optional, "id" is a must. It should not matter where after the "embed_viewat" the parameters are: width could be at the beginning, in the middle or the end, as could any other parameter (this is the trickiest thing for me and regex). b) Parse the string and extract the parameters. Desciption: I would like to have the parameter values in an array for later use. Then I test if at least an "id" is given, otherwise there will be an error message. c) Pre-write the command for the other plugin in a variable using the above parameters. d) Replace the whole string "[embed_viewat.....]" in the post contents with the command in c) and save the post. The problem is definetly the regular expressions to use with preg_replace or whatever. I have tried this one (with regextester.co.nr): ~\[embed_viewat (width=[0-9]+)|(height=[0-9]+)|(id=[0-9]+)|(lang=[a-z]+)\]~ Well, it does not really work... I tried the example under a): [embed_viewat width=600 height=400 id=1234 lang=de] This leads to the following haystack: Array ( [0] => Array ( [0] => [embed_viewat width=600 [1] => height=400 [2] => id=1234 [3] => lang=de] ) [1] => Array ( [0] => width=600 [1] => [2] => [3] => ) [2] => Array ( [0] => [1] => height=400 [2] => [3] => ) [3] => Array ( [0] => [1] => [2] => id=1234 [3] => ) [4] => Array ( [0] => [1] => [2] => [3] => lang=de ) ) whereas I think I will need something like this: ( [0] => [embed_viewat width=600 height=400 id=1234 lang=de] [1] => width=600 [2] => height=400 [3] => id=1234 [4] => lang=de ) I mean, I need the whole string AND THEN the parameters. Does that make sense? This one [embed_viewat height=400 width=600 lang=de id=1234] leads to: Array ( [0] => Array ( [0] => height=400 [1] => id=1234 ) [1] => Array ( [0] => [1] => ) [2] => Array ( [0] => height=400 [1] => ) [3] => Array ( [0] => [1] => id=1234 ) [4] => Array ( [0] => [1] => ) ) whereas I would (again) need something like this: ( [0] => [embed_viewat height=400 width=600 lang=de id=1234] [1] => height=400 [2] => width=600 [3] => lang=de [4] => id=1234 ) I also posted this in the wordpress support forum for those who are interested: http://wordpress.org/support/topic/172726 I'd be grateful for any help on this. Thanks! Quote Link to comment Share on other sites More sharing options...
aCa Posted April 30, 2008 Share Posted April 30, 2008 Hi Would this regex solve your problem? \[embed_viewat(( width=[0-9]+)|( height=[0-9]+)|( id=[0-9]+)|( lang=[a-z]+))+\] It doesn't give you exactly the array like you wanted but it gives you the values you need. Will f.eks return: Array ( [0] => Array ( [0] => [embed_viewat height=400 width=600 lang=de id=1234] ) [1] => Array ( [0] => id=1234 ) [2] => Array ( [0] => width=600 ) [3] => Array ( [0] => height=400 ) [4] => Array ( [0] => id=1234 ) [5] => Array ( [0] => lang=de ) ) Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 30, 2008 Share Posted April 30, 2008 Simply add the PREG_SET_ORDER flag at the end of your preg_match_all call. It'll format it more like what Saphod expects... Quote Link to comment Share on other sites More sharing options...
Saphod Posted May 6, 2008 Author Share Posted May 6, 2008 OK, nice, this gives me a single array with all the results - thanks. I also thought about doing a step-by-step regex with preg_match rather than getting everything with preg_match_all all at once: a) search for the whole "[embed_viewat(.*)]" string b) search for "width" c) search for "height" d) search for "id" e) search for "lang" I think this makes it easier even to respond with a possible error message, right? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.