使用 preg_replace 识别并替换 PHP 中字符串中的短代码变量对
Identify and replace pairs of shortcoded variables within a string in PHP using preg_replace
在有人问之前,我花了一个小时试图找到关于 SO 的解决方案,但似乎没有什么能完全达到我想要的...如果它在那里,我道歉!
所以...我正在尝试 运行 preg_replace 一堆文本来替换短代码对。
示例文本:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]Abc123[/img][cap]This is an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]321cbA[/img][cap]This is also an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
我想把每张图片和标题放在一起,然后这样替换它们:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
<img src="Abc123.jpg" alt="This is an image" />
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
<img src="321cbA.jpg" alt="This is also an image" />
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
我需要的代码如下:
$text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]Abc123[/img][cap]This is an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]321cbA[/img][cap]This is also an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.";
$pattern = '/\[(img)\](.*?)\[\/\]?/';
// This doesn't work: $pattern = '/\[(img)\](.*?)\[\/\]\[(cap)\](.*?)\[\/\]?/';
$replacement = '<img src=".jpg" alt="Help" />';
// "Help" will be when we get both values
echo preg_replace($pattern, $replacement, $text);
此代码获取一个值,但我终其一生都无法获取同时获取两个值的模式(上面的注释行是我所能得到的)。我已经尝试了数十个 SO 页面和其他网站,但似乎没有任何效果。毫无疑问,更正后的模式会很简单,但如果有人能让我摆脱痛苦,我将不胜感激!
感谢您的宝贵时间。
$str = <<<'EOD'
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]Abc123[/img][cap]This is an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]321cbA[/img][cap]This is also an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
EOD;
$trans = ['[img]' => '<img src="', '[/img][cap]' => '" alt="', '[/cap]' => '" />'];
echo strtr($str, $trans);
此外,您代码中的最后一个引用应该是 </code> 而不是 <code>
(但您真的需要捕获标签名称吗?)。
我已经将你的代码改编成这样:
$text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]Abc123[/img][cap]This is an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]321cbA[/img][cap]This is also an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.";
$pattern = '/\[img\](\w+)\[\/img\]\[cap\]([\w\s]+)\[\/cap\]/';
// This doesn't work: $pattern = '/\[(img)\](.*?)\[\/\]\[(cap)\](.*?)\[\/\]?/';
$replacement = '<img src=".jpg" alt="" />';
// "Help" will be when we get both values
echo preg_replace($pattern, $replacement, $text);
我只更改了模式和替换字符串。
在有人问之前,我花了一个小时试图找到关于 SO 的解决方案,但似乎没有什么能完全达到我想要的...如果它在那里,我道歉!
所以...我正在尝试 运行 preg_replace 一堆文本来替换短代码对。
示例文本:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]Abc123[/img][cap]This is an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]321cbA[/img][cap]This is also an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
我想把每张图片和标题放在一起,然后这样替换它们:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
<img src="Abc123.jpg" alt="This is an image" />
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
<img src="321cbA.jpg" alt="This is also an image" />
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
我需要的代码如下:
$text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]Abc123[/img][cap]This is an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]321cbA[/img][cap]This is also an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.";
$pattern = '/\[(img)\](.*?)\[\/\]?/';
// This doesn't work: $pattern = '/\[(img)\](.*?)\[\/\]\[(cap)\](.*?)\[\/\]?/';
$replacement = '<img src=".jpg" alt="Help" />';
// "Help" will be when we get both values
echo preg_replace($pattern, $replacement, $text);
此代码获取一个值,但我终其一生都无法获取同时获取两个值的模式(上面的注释行是我所能得到的)。我已经尝试了数十个 SO 页面和其他网站,但似乎没有任何效果。毫无疑问,更正后的模式会很简单,但如果有人能让我摆脱痛苦,我将不胜感激!
感谢您的宝贵时间。
$str = <<<'EOD'
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]Abc123[/img][cap]This is an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]321cbA[/img][cap]This is also an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
EOD;
$trans = ['[img]' => '<img src="', '[/img][cap]' => '" alt="', '[/cap]' => '" />'];
echo strtr($str, $trans);
此外,您代码中的最后一个引用应该是 </code> 而不是 <code>
(但您真的需要捕获标签名称吗?)。
我已经将你的代码改编成这样:
$text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]Abc123[/img][cap]This is an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
[img]321cbA[/img][cap]This is also an image[/cap]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.";
$pattern = '/\[img\](\w+)\[\/img\]\[cap\]([\w\s]+)\[\/cap\]/';
// This doesn't work: $pattern = '/\[(img)\](.*?)\[\/\]\[(cap)\](.*?)\[\/\]?/';
$replacement = '<img src=".jpg" alt="" />';
// "Help" will be when we get both values
echo preg_replace($pattern, $replacement, $text);
我只更改了模式和替换字符串。