简体   繁体   English

创建短代码的方法,用PHP添加html打开和关闭标签到字符串

[英]Method to create shortcode to add html opening and closing tags to string with php

I'm not sure if I am phrasing this right as it's not exactly shortcodes. 我不确定我是否正在措辞,因为它不是完全短码。

What I am trying to achieve is to create a function that find a replaces asterisks in my strings. 我想要实现的是创建一个函数,找到替换我的字符串中的星号。 But I need to alternate first and second replace. 但我需要替换第一和第二替换。

$srting = 'Create personalised *tasty treats*';

I need to account for multiple uses of it, for example like this string below... 我需要考虑它的多种用途,例如下面这个字符串......

$srting = 'Create personalised *tasty treats* and branded *promotional products*';

The first * will be replaced with a opening <span class="d-inline-block"> 第一个*将替换为开头<span class="d-inline-block">

The second * will be replaced with a closing </span> 第二个*将替换为结束</span>

And the the cycle repeats again for any more uses of * in the string. 并且对于字符串中*任何更多用途,循环再次重复。

I'm not sure what the most efficient way is to approach this, is this something that could be done with regex? 我不确定最有效的方法是什么,这是可以用正则表达式完成的吗? Any ideas would be great thanks. 任何想法都会非常感谢。


Updated working function below using accepted answer. 使用已接受的答案更新了下面的工作

public static function word_break_fix($string) {

   $newString = preg_replace('/\\*([^*]*)\\*/s', '<span class="d-inline-block">\\1</span>', $string);

   return $newString;

}

Just use preg_replace capturing everything between two asterisks. 只需使用preg_replace捕获两个星号之间的所有内容。 You can reference a capturing group from the replacement by a backslashed number. 您可以通过反斜杠编号从替换中引用捕获组。

preg_replace('/\\*([^*]*)\\*/s', '<span class="d-inline-block">\\1</span>', $subject)

https://regex101.com/r/i7fm8X/1/ https://regex101.com/r/i7fm8X/1/

Note that in PHP regular expressions are built by strings, thus you escape characters once for the regex and the backslash is escaped again when using string literals. 请注意,在PHP中,正则表达式是由字符串构建的,因此您可以为正则表达式转义一次字符,并在使用字符串文字时再次转义反斜杠。

Yes, this is absolutely something ideally suited for regex! 是的,这绝对是正则表达式的理想选择!

For tag replacement, something like this works well: 对于标签替换,这样的东西效果很好:

<?php

$string = 'Create personalised *tasty treats* and branded *promotional products* *tasty treats*';

$replace = preg_replace_callback("/(\\*[a-zA-Z\\s]*\\*)/m", function ($matches) {
    switch($matches[0])
    {
        case "*tasty treats*":
            return "Chocolate!";
        case "*promotional products*":
            return "DRINK COCA COLA!";
    }
    return $matches[0];
}, $string);

echo $replace;

Here's a link to Regex101 so you can see and learn how the regex works: https://regex101.com/r/pyCTZU/1 这里是Regex101的链接,因此您可以查看和了解正则表达式的工作原理: https ://regex101.com/r/pyCTZU/1

But to inject HTML as you specified, try this: 但要按照您的指定注入HTML,请尝试以下操作:

<?php

$string = 'Create personalised *tasty treats* and branded *promotional products* *tasty treats*';

$replace = preg_replace_callback("/(\\*[a-zA-Z\\s]*\\*)/m", function ($matches) {

    return "<span class=\"d-inline-block\">".substr($matches[0], 1, strlen($matches[0]) - 2)."</span>";

}, $string);

echo $replace;

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

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