简体   繁体   English

从字符串创建字符串数组

[英]Creating an array of strings from a string

I have a long string. 我有一长串。 Within that string there are certain characters I need to watch for and process: 在该字符串中,我需要注意和处理某些字符:

$lyrics = "This is an {G}example of a {D}long string.  It's {Em}getting {C}longer and {Am}longer the more I {Dm7-5}type"

What I want to do is find the occurrences of the curly braces, and if the LENGTH of the string I find in between the curly braces is less than X, do thing A and if it's equal to or greater than X, do thing B. 我想做的是查找大括号的出现,如果我在大括号之间找到的字符串的长度小于X,则执行A;如果它等于或大于X,则执行B。

Both thing A and thing B involve changing the curly braces to HTML. 事物A和事物B都涉及将花括号更改为HTML。

I've been searching and thinking about this problem for many days now and I've yet to find an approach that's succinct and simple. 我已经搜索和思考这个问题很多天了,但是我还没有找到一种简洁明了的方法。

Of course I could treat the string as an array and loop through - but really? 当然,我可以将字符串视为数组并循环遍历-但是真的吗? Is this really a matter of me starting with the first character in $lyrics and marching through to the end? 这真的是我一个问题,从$ lyrics中的第一个字符开始一直走到最后吗?

Is there an obviously simpler solution I'm missing? 我是否有一个明显更简单的解决方案? Would regular expressions help? 正则表达式有帮助吗? I know what regular expressions are, but I confess I've not had to work with them much. 我知道什么是正则表达式,但我承认我不必过多地使用它们。

I agree with the others, regular expressions are the way to go. 我同意其他观点,正则表达式是解决之道。 Here some examples that might help out. 这里有一些例子可能会有所帮助。

You didn't define what your tags were supposed to do so let's use this string for the example. 您没有定义标签应该执行的操作,因此让我们使用此字符串作为示例。 One addition I've made is the {sp###}...{/sp} start/end tags which will give you more control over your formatting. 我添加的一个附加功能是{sp ###} ... {/ sp}的开始/结束标记,它使您可以更好地控制格式。

$str = "{sp5}{em}Italics {size30}ARE{/sp} {sp18}{b}fun.{/sp}";

The thing to know about pattern matching in PHP is that by putting part of the pattern inside of parenthesis it will set it to a variable that you can re-use in your string replacement. 关于PHP中的模式匹配,要知道的是,通过将模式的一部分放在括号内,会将其设置为一个变量,您可以在字符串替换中重复使用该变量。

Single word tags 单字标签

// emphasize the next word
$str = preg_replace("/\{em\}(\w+)\b/","<em>$1</em>",$str);

Here we look for an {em} followed by one or more word characters \\w+ followed by a word boundary. 在这里,我们寻找一个{em},后跟一个或多个单词字符\\ w +,后跟一个单词边界。 A word character is any letter, number, or underscore. 文字字符是任何字母,数字或下划线。 The \\w+ is in parenthesis so it will get set to the variable $1 and re-used in the second part which places it inside HTML em tags. \\ w +用括号括起来,因此将其设置为变量$ 1并在第二部分中重新使用,将其放置在HTML em标签中。 Any other patterns in parenthesis will be named iteratively. 括号中的任何其他模式将被迭代命名。 ($1 $2 $3 $4) ($ 1 $ 2 $ 3 $ 4)


// Bold the next word if bracket content is 1 character
$str = preg_replace("/\{\w\}(\w+)\b/","<b>$1</b>",$str);

Here instead of searching for {em} specifically we search for ANY single word character . 在这里,我们不是搜索{em}而是搜索任何单个单词character It only matches it if it is a single character because there is no plus sign after the \\w. 仅当它是单个字符时才匹配它,因为\\ w后面没有加号。


// bracket variables 
$str = preg_replace("/\{size(\d+)\}(\w+)\b/","<span style='font-size: $1px;'>$2</span>",$str);

In this example we set the CSS font-size of the next word to a specified amount. 在此示例中,我们将下一个单词的CSS字体大小设置为指定的数量。 After {size we look for one-or-more of any digit which is set to the variable $1. 在{size之后,我们寻找设置为变量$ 1的任何一位或多位。 Then the next series of word characters after the are set to $2. 然后将后面的下一个单词字符系列设置为$ 2。

Start/End tags 开始/结束标签

// Letter Spacing 
$str = preg_replace("/\{sp(\d+)\}(.+)\{\/sp\}/U","<span style='letter-spacing: $1px;'>$2</span>",$str);

This one is much like the last, with 3 exceptions. 这很像最后一个,有3个例外。 Rather than searching for one-or-more word characters we search for ANY character with the dot/period wildcard. 而不是搜索一个或多个单词字符,我们搜索带点/句点通配符的任何字符。 Also, we don't terminate with a word boundary. 另外,我们不以单词边界结尾。 We search for the string "{/sp}". 我们搜索字符串“ {/ sp}”。 And finally, we set the internal option U, which stands for Ungreedy. 最后,我们设置内部选项 U,代表Ungreedy。 We must set this because the ".+" pattern matches {/sp}; 我们必须设置它,因为“。+”模式匹配{/ sp}; Ungreedy allows it to match the first occurrence of the end tag if the rest of the pattern matches. 如果其余模式匹配,则Ungreedy允许它匹配end标签的第一次出现。

Finally 最后

I hope this will get you on your way in the wonderful world of regular expressions! 希望这能带您进入正则表达式的美好世界!

These should help. 这些应该会有所帮助。

http://www.php.net/manual/en/reference.pcre.pattern.syntax.php http://www.php.net/manual/zh/reference.pcre.pattern.syntax.php

http://www.regextester.com/ http://www.regextester.com/

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

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