0

I am looking for help with regular expression $pattern to convert inline image tags like [image:123:title:size] into HTML img tags.

here is the code:

//[image:ID:caption:size]
$content = '[image:38:title:800x900]';

preg_match_all( '/\[image:(\d+)(:?)([^\]]*)\]/i', $content, $images );

        if( !empty( $images[0] ) )
        {   // There are image inline tags in the content
            foreach( $images[0] as $i => $tag )
            {

            $link_ID = (int)$images[1][$i];
            $caption = empty( $images[2][$i] ) ? '#' : $images[3][$i];
            $size = empty( $images[4][$i] ) ? '#' : $images[5][$i];

            }
            echo '<br />';
            echo 'ID: '.$link_ID.'<br />';
            echo 'Tag: '.$caption.'<br />';
            echo 'size: '.$size.'<br />';
        }

which outputs:

image id: 12

Title: caption:size

size: #

but should output this:

image id: 12

Title: caption

size: size

this---> /[image:(\d+)(:?)([^]]*)]/i

does not work

Any help would be great!

4

2 回答 2

0

这是你要找的东西吗?我假设您正在进行内联解析,所以 preg_replace 可能会做得更好。不过,我不确定您要执行的操作的确切细节。

<?php

$content = 'Check out my awesome [image:38:title:800x900], but not as good as my other [image:20:thumbnail:200x200]';

$parsed_content = preg_replace( '/\[image:(\d+):([^\:]+):(\d+)x(\d+)\]/i', '<img src=\'$1.jpg\' alt=\'$2\' width=$3 height=$4>', $content);

echo "Before: {$content}\n";
echo "After: {$parsed_content}\n";

输出:

之前:看看我的真棒[image:38:title:800x900],但不如我的其他[image:20:thumbnail:200x200]

之后:看看我的真棒 <img src='38.jpg' alt='title' width=800 height=900>,但不如我的其他<img src='20.jpg' alt='thumbnail' width=200 height=200>

编辑:

<?php
$content = '[image:38:title:800x900]';

preg_match_all( '/\[image:(?<id>\d+):(?<caption>[^:]+):(?<size>[\dx]+)/i', $content, $images );

        if( !empty( $images[0] ) )
        {   // There are image inline tags in the content
            foreach( $images[0] as $i => $tag )
            {

            $link_ID = (int)$images['id'][$i];
            $caption = empty( $images['caption'][$i] ) ? '#' : $images['caption'][$i];
            $size = empty( $images['size'][$i] ) ? '#' : $images['size'][$i];

            }
            echo '<br />' . "\n";
            echo 'ID: '.$link_ID.'<br />' . "\n";
            echo 'Tag: '.$caption.'<br />' . "\n";
            echo 'size: '.$size.'<br />' . "\n";
        }
于 2013-10-19T22:21:40.913 回答
0

解决了!

$content = '[image:12:caption:size]';
preg_match_all( '/\[image:(\d+)(:?)(.*)(:)([^\]]*)\]/i', $content, $images );

        if( !empty( $images[0] ) )
        {   // There are image inline tags in the content
            foreach( $images[0] as $i => $tag )
            {

                $link_ID = (int)$images[1][$i];
                $caption = empty( $images[2][$i] ) ? '#' : $images[3][$i];
                $size = empty( $images[4][$i] ) ? '#' : $images[5][$i];
                print_r($images);
            }
            echo '<br />';
            echo 'ID: '.$link_ID.'<br />';
            echo 'Title: '.$caption.'<br />';
            echo 'Dimensions: '.$size.'<br />';
        }

替换:

/\[image:(\d+)(:?)([^\]]*)\]/i

和:

/\[image:(\d+)(:?)(.*)(:)([^\]]*)\]/i

谢谢你!

于 2013-10-20T06:59:24.403 回答