Solved

PHP Regex question

Posted on 2011-03-24
4
506 Views
Last Modified: 2012-05-11
Hello all,
 I'm having problem with php regex. What I want to do is;

$string = "[test-numberTest12-{anotherValue2}]some text here[/test]";

Open in new window


I want my preg_match to match these values. -numberTest12- and {anotherValue2} should be case-insensitive, can contain numbers or letters anywhere (numbers and letters can be anywhere -beginning, end, middle...-)

What I try is a bit like BBCode but I'm not using them as BBCode, that's why I would like to make a underline here, this is not a BBCode but the logic is more or less the same.

I'm not good with regex but I tried;
preg_match("/\[test-([0-9]|[a-z])-\]/", $string)

Open in new window

Doesn't work. I couldn't even get the first part done so I could jump to second value. I will be glad if anyone could help me out with this problem.
0
Comment
Question by:pixalax
  • 2
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35205935
Try this code
<?php

$string = "[test-numberTest12-{anotherValue2}]some text here[/test]";

$pattern = '#.*\[test([^\{]+)\{(.+)\}\]#';

preg_match( $pattern, $string, $matches );

print_r( $matches );

Open in new window

0
 
LVL 2

Author Comment

by:pixalax
ID: 35205967
Hello bportlock;
 I have no idea what you wrote in $pattern but it works. If I might ask you to explain me a bit would it be too much?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35206057
"I have no idea what you wrote in $pattern but it works"

:-D   That's regexes for you......


Let's look at the pattern '#.*\[test([^\{]+)\{(.+)\}\]#' and we will skip the delimiters so we start with

.*\[test([^\{]+)\{(.+)\}\]

That splits up into the following logical sections

.*   \[test   ([^\{]+)   \{  (.+)  \}   \]

The first bit .* says 'there may be characters at the start

Next we have \[test and since [ is a normal regex character we need to escape it with a \ to indicate that it is DATA and not part of the regex controls. test is just the word 'test'. So this bit looks for data matching [test

Next we have ([^\{]+) which is a capture group (inside brackets) that is looking for any data that is NOT a curly brace and that data must occur at  least once (the plus sign)

Next we look for an opening brace \{

Next we look for at least one character (.+)

Next we look for a closing brace \}

and that's pretty much it
0
 
LVL 2

Author Closing Comment

by:pixalax
ID: 35206114
Wow, thank you very much. This is summing up lot's of things for me.

Before I had to use spaces in order to grab [xxx] now I realize with your help i don't need spaces at all, I can use .*\

(If I was using [xxx]text[/xxx][yyy]text[/yyy] it didn't work, I had to use spaces so my regex could grab them.

Thanks a billion for your fast reply and explanation.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mysqli_connect error on wamp but not on remote server 8 38
php help 34 58
Row insertion failed. Array 5 48
Delete  php pages of a part of our site 8 34
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 (http://en.wikipedia.org/wiki/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.

830 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