Please login or register.

Login with username, password and session length
Advanced search  

News:

(2009-05-04) The Rules and Terms of Service have been updated. Please make sure you read, understand, and follow them.

Author Topic: HowTo Replace A Pseudo-Tag With Text  (Read 728 times)

0 Members and 1 Guest are viewing this topic.

Saphod

  • Irregular
  • Offline Offline
  • Posts: 17
    • View Profile
    • WWW
HowTo Replace A Pseudo-Tag With Text
« 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: [Select]
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: [Select]
(
    [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: [Select]
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: [Select]
(
    [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!
Logged

aCa

  • Irregular
  • Offline Offline
  • Posts: 12
    • View Profile
Re: HowTo Replace A Pseudo-Tag With Text
« Reply #1 on: April 30, 2008, 04:13:27 AM »
Hi
Would this regex solve your problem?
Code: [Select]
\[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: [Select]
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
        )

)
Logged
Check out my regex tool at http://regex.larsolavtorvik.com/

discomatt

  • Addict
  • Offline Offline
  • Gender: Male
  • Posts: 1,936
    • View Profile
Re: HowTo Replace A Pseudo-Tag With Text
« 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...
Logged

Saphod

  • Irregular
  • Offline Offline
  • Posts: 17
    • View Profile
    • WWW
Re: HowTo Replace A Pseudo-Tag With Text
« 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?
Logged

PHP Freaks Forums

 
 
 

Page created in 0.079 seconds with 22 queries.