Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 简化Wordpress短代码(或“自定义短代码”)_Php_Regex_Wordpress_Preg Match - Fatal编程技术网

Php 简化Wordpress短代码(或“自定义短代码”)

Php 简化Wordpress短代码(或“自定义短代码”),php,regex,wordpress,preg-match,Php,Regex,Wordpress,Preg Match,我使用一个短代码在我的Wordpress模板上显示我的图像 [photo id="13927101803" caption="Some text"] // Flickr [photo id="mqEqHAC3rK"] // Instagram, no caption 因为我是用降价方式写文章的,所以当我有10张或更多照片要包含时,写这些[attr=“ibutes”]有点无聊。 这就是我希望简化这些照片的原因,包括: [13927101803 "Caption"] // Flickr [mqEq

我使用一个短代码在我的Wordpress模板上显示我的图像

[photo id="13927101803" caption="Some text"] // Flickr
[photo id="mqEqHAC3rK"] // Instagram, no caption
因为我是用降价方式写文章的,所以当我有10张或更多照片要包含时,写这些[attr=“ibutes”]有点无聊。 这就是我希望简化这些照片的原因,包括:

[13927101803 "Caption"] // Flickr
[mqEqHAC3rK] // Instagram

// Causes a conflict with the links in Markdown [this is](http://a-link)… ?
当我保存一篇文章时,它会运行一个钩子来检测短代码,调用Flickr/Instagram API来获取源图像url并将其存储到数据库中(每个文章都有一个照片数组)

以下是我当前捕获短代码的代码:

有没有办法简化这些电话?或者另一个想法,快速调用它,不要使用这些长短码?

好的,给你:

/\[(\d{11}|\w{10})(?:\s*"([^"]+)")?\]\/
假设您有以下内容:

简单测试[13927101801“标题”][mqEqHAC3rK“标题”]简单 简单测试简单测试简单测试简单测试 简单测试简单测试A[13927101801]

然后你可以这样做:

$content = do_shortcode(get_the_content());
preg_match_all('/\[(\d{11}|\w{10})(?:\s*"([^"]+)")?\]\/', $content, $matches);

$matches = array_slice($matches, 1);
$matches = call_user_func_array( 'array_map', array_merge(array(NULL), $matches) );

var_dump($matches);
输出:

array (size=3)
  0 => 
    array (size=2)
      0 => string '13927101801' (length=11) // <-- ID
      1 => string 'Caption' (length=7)      // <-- Caption content
  1 => 
    array (size=2)
      0 => string 'mqEqHAC3rK' (length=10) // <-- ID
      1 => string 'caption' (length=7)     // <-- Caption content
  2 => 
    array (size=2)
      0 => string '13927101801' (length=11) // <-- ID
      1 => string '' (length=0)           // <-- No Caption content
数组(大小=3)
0 => 
数组(大小=2)
0=>string'13927101801'(长度=11)//string'Caption'(长度=7)//
数组(大小=2)
0=>string'mqqhac3rk'(长度=10)//string'caption'(长度=7)//
数组(大小=2)

0=>字符串“13927101801”(长度=11)//字符串“”(长度=0)//用简单的词解释。您试图实现的目标是什么?使用此
[13927101803“Caption”]
而不是此
[photo id=“13927101803”Caption=“Some text”]
:)此
13927101803
和此
mqEqHAC3rK
将在以后更改?这些ID是针对单个图像的吗?是的,每个图像的ID都会发生变化,但flickr的ID始终为11位,InstagramNo的ID始终为10位[a-zA-Z0-9]您不能。Wordpress短代码必须具有唯一的占位符来描述短代码,就像您使用的
[photo…]
一样。这就是为什么要使用属性来修改动态数据
key/value
pairs.wow!这真的是我的技能问题。我可以像读中文一样读你的正则表达式,或者如果我的猫坐在我的键盘上……无论如何,谢谢你的建议,我会尽快测试它。好的,你的正则表达式模式是一块金块,它就像一个符咒!谢谢你的帮助:)