Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/103526-howto-replace-a-pseudo-tag-with-text/
Share on other sites

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
        )

)

 

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.