简体   繁体   English

preg_match_all:匹配自定义标签

[英]preg_match_all: Match custom tags

I'm currently playing around a bit with regular expressions and want to implement some kind of custom tags for texts on a site of mine. 我目前正在使用正则表达式,希望在我的站点上为文本实现某种自定义标签。 For example, if I want to implement a picture into a text, I use the following bracket-tag to do so… 例如,如果我想将图片实现为文本,请使用以下方括号标记来实现……

Lorem ipsum dolor sit amet (image: tiger.jpg width: 120 height: 200 title: This picture shows a tiger) sed diam nonumy eirmod tempor invidunt Lorem ipsum dolor sit amet (图片:tiger.jpg宽度:120高度:200标题:此图片显示为老虎) sed diam nonumy eirmod tempor invidunt

Now I want my PHP-script to 1. find these bracket-tags and 2. read the single tags in this brackets, so I get some kind of an array like… 现在,我希望我的PHP脚本1.找到这些括号标记,并2.读取此括号中的单个标记,因此我得到了某种类似于...的数组。

$attributes = array(
    'image' => 'tiger.jpg',
    'width' => '150',
    'height' => '250',
    'title' => 'This picture shows a tiger',
);

The (for me) tricky part about this is that the "values" can contain everything as long it doesn't contain something like (\\w+)\\: – because this is the start of an different tag. (对我而言)棘手的部分是,“值”可以包含所有内容,只要它不包含(\\w+)\\: – –因为这是另一个标记的开始。 The following snippet represents what I have so far – finding the brackets-things works so far, but splitting the bracket-contents into the single tags doesn't really work. 下面的代码段代表了我到目前为止所拥有的内容–到目前为止,找到方括号内的内容仍然有效,但是将方括号内容拆分为单个标签并不能真正起作用。 I used (\\w+) for matching the value just as a placeholder – this would not match "tiger.jpg" or "This picture shows a tiger" or something else. 我用(\\w+)作为占位符来匹配值–这与“ tiger.jpg”或“此图片显示老虎”或其他内容不匹配。 I hope you understand what I mean! 希望你明白我的意思! ;) ;)

<?php

$text = 'Lorem ipsum dolor sit amet (image: tiger.jpg width: 150 height: 250 title: This picture shows a tiger) sed diam nonumy eirmod tempor invidunt';

// find all tag-groups in brackets
preg_match_all('/\((.*)\)/s', $text, $matches);

// found tags?
if(!empty($matches[0])) {

    // loop through the tags
    foreach($matches[0] as $key => $val) {

        $search = $matches[0][$key]; // this will be replaced later
        $cache = $matches[1][$key]; // this is the tag without brackets

        preg_match_all('/(\w+)\: (\w+)/s', $cache, $list); // find tags in the tag-group (i.e. image, width, …)

        echo '<pre>'.print_r($list, true).'</pre>';

    }

}

?>

Would be great if somebody could help me out of this! 如果有人可以帮助我,那就太好了! Thanks! 谢谢! :) :)

<?

$text = 'Lorem ipsum dolor sit amet (image: tiger.jpg width: 150 height: 250 title: This picture shows a tiger) sed diam nonumy eirmod tempor invidunt';

// find all tag-groups in brackets
preg_match_all('/\(([^\)]+)\)/s', $text, $matches);
$attributes = array();

// found tags?
if ($matches[0]) {
    $m = preg_split('/\s*(\w+)\:\s+/', $matches[1][0], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    for ($i = 0; $i < count($m); $i+=2) $attributes[$m[$i]] = $m[$i + 1];
}

var_export($attributes);

/*
array (
  'image' => 'tiger.jpg',
  'width' => '150',
  'height' => '250',
  'title' => 'This picture shows a tiger',
)
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM