Help with PHP Regex...

Posted on 2011-10-14
Last Modified: 2012-05-12
I am using the following regex to extract tags from text that appear in curly brackets {}. It works fine if the only text is the tag, such as {img:Src} but if there is anything else in there it doesn't work. so '{img:src} and text' doesn't work.
$value = '{img:Src} some text {border:Solid} more text {img:Lrg}';
preg_match_all ('/^\{.*\}$/', $value, $foo);
trigger_error(print_r($foo, true));
//Should be an array of 3 items: {img:Src}, {border:Solid}, {img:Lrg}

Open in new window

Question by:interclubs
    LVL 107

    Expert Comment

    by:Ray Paseur
    Try removing the ^ and $ from it.
    LVL 107

    Accepted Solution

    This seems to work.
    <?php // RAY_temp_interclubs.php
    $rgx = '/\{(.*?)\}/';
    $value = '{img:Src} some text {border:Solid} more text {img:Lrg}';
    preg_match_all ($rgx, $value, $foo);

    Open in new window


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now