简体   繁体   中英

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.

Both thing A and thing B involve changing the curly braces to 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?

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.

$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.

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. 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. Any other patterns in parenthesis will be named iteratively. ($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 . It only matches it if it is a single character because there is no plus sign after the \\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. After {size we look for one-or-more of any digit which is set to the variable $1. Then the next series of word characters after the are set to $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. 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}". And finally, we set the internal option U, which stands for Ungreedy. We must set this because the ".+" pattern matches {/sp}; Ungreedy allows it to match the first occurrence of the end tag if the rest of the pattern matches.

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.regextester.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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