Solved

PHP Regex question

Posted on 2011-03-24
4
502 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=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…
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.

757 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

20 Experts available now in Live!

Get 1:1 Help Now