August 21, 2008, 02:19:22 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: We are constantly trying to improve phpfreaks and these forums, so feel free to go to the PHPFreaks Comments/Suggestions board and point out anything you'd like to see different!
 
   Home   Help Search Calendar Rules Staff List Login Register  
Pages: [1]
  Send this topic  |  Print  
Author Topic: HowTo Replace A Pseudo-Tag With Text  (Read 462 times)
0 Members and 1 Guest are viewing this topic.
Saphod
Member
Offline Offline

Posts: 17



View Profile WWW
« on: April 30, 2008, 02:30:34 AM »

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:
Code:
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:

Code:
(
    [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:

Code:
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:

Code:
(
    [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!
Report to moderator   Logged
aCa
Member
Offline Offline

Posts: 12


View Profile
« Reply #1 on: April 30, 2008, 04:13:27 AM »

Hi
Would this regex solve your problem?
Code:
\[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:
Code:
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
        )

)
Report to moderator   Logged

Check out my regex tool at http://regex.larsolavtorvik.com/
discomatt
Member
Offline Offline

Posts: 1,313


View Profile
« Reply #2 on: April 30, 2008, 08:54:04 AM »

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...
Report to moderator   Logged
Saphod
Member
Offline Offline

Posts: 17



View Profile WWW
« Reply #3 on: May 06, 2008, 01:17:52 PM »

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?
Report to moderator   Logged
Pages: [1]
  Send this topic  |  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.049 seconds with 17 queries.