【问题标题】:Convert inline image tags like [image:123:title:size] into HTML img tags将 [image:123:title:size] 等内联图像标签转换为 HTML img 标签
【发布时间】:2013-10-28 14:07:57
【问题描述】:

我正在寻求有关正则表达式 $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

标题:说明

尺寸:尺寸

这---> /[图像:(\d+)(:?)([^]]*)]/i

没用

任何帮助都会很棒!

【问题讨论】:

  • 什么添加了那种图像“标签”??
  • 它是其他代码的一部分。我需要 3 个变量来进一步转换。
  • 这是一个从帖子中提取内容并查找该模式然后将其转换为图像标签的函数。
  • 对于您给出的示例 ([image:38:title:800x900]),处理后的 &lt;img/&gt; 会是什么样子? &lt;img src="38.jpg" alt="title" width=800 height=900&gt;?

标签: php html arrays image preg-match-all


【解决方案1】:

这是您正在寻找的东西吗?我假设您正在进行内联解析,所以 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]

之后:看看我的真棒 &lt;img src='38.jpg' alt='title' width=800 height=900&gt;,但没那么好 作为我的另一个&lt;img src='20.jpg' alt='thumbnail' width=200 height=200&gt;

编辑:

<?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";
        }

【讨论】:

  • 感谢您的回复,但这不是我想要的。我只是想要正确的表达式将其拆分为 ":" /[image:(\d+):([^\:]+):(\d+)x(\d+)]/i 以便它输出: image id :12 标题:标题大小:大小而不是图像 id:12 标题:标题:大小大小:#
  • 我已经用你的原始代码更新了答案,现在我认为你正在追求它。
【解决方案2】:

已解决!

$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

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    相关资源
    最近更新 更多