Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7649159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:53:36+00:00 2026-05-31T10:53:36+00:00

I have a long string. Within that string there are certain characters I need

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:53:37+00:00Added an answer on May 31, 2026 at 10:53 am

    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long string (multiple paragraphs) which I need to split into a
I have a long string of comments that I'd like to split into multiple
I have a long string, ex: Please help me to solve this problem. This
I have a couple of classes (for this example anyway) that use code first
I have a need for a high-performance string hashing function in python that produces
I have a long string of HTML that contains <p> <img> <span> and a
I have a long string (a DNA sequence). It does not contain any whitespace
i have a long string variable and i want to search in it for
i have a long running function¹: public string FindPasswordFromHash(String hash) { ... } which
I have a long sequence of hex digits in a string, such as 000000000000484240FA063DE5D0B744ADBED63A81FAEA390000C8428640A43D5005BD44

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.