将内联图像标签(如 [image:123:title:size] 转换为 HTML img 标签


Convert inline image tags like [image:123:title:size] into HTML img tags

我正在寻找正则表达式$pattern的帮助,以将内联图像标签(如 [image:123:title:size] 转换为 HTML img 标签)。

这是代码:

//[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 />';
        }

其中输出:

图像 ID:12

标题:标题:大小

大小: #

但应该输出这个:

图像 ID:12

标题

:标题

尺寸

:尺寸

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

不起作用

任何帮助都会很棒!

这是你要找的那种东西吗?我假设您正在进行内联解析,因此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";
        }

解决了!

$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

谢谢!