Page 1 of 1

Simple RegEx To find a number in a special tag

Posted: Tue May 12, 2009 5:00 pm
by socket1
As I have absolutely no experience with RegEx whatsoever but I require a simple RegEx to match this
Say I have an html page with at various points there are things like {img=20} or {img=14} I need a regex to return an array like this:
Array
(
[0] => 20
[1] => 14
)
So I can preform foreach on the array and replace the {img=id} tags with something like <img src="image.php?id=20" />

I realize this can be done using preg_match('regex pattern', $the_html_page, $matches) but simply have no idea how to approach this.

It also needs to be able to recognize the tags not matter what they have around them like text</p>{img=id}<p>more text
It would have to regognize from 1 digit to as many possible in the id part too.
Could there be a way of matching an id or a string of text like {img=computer1} so it would be like <img src="image.php?id=computer1" /> so the image.php could query the db by ID or a string identifier.

Re: Simple RegEx To find a number in a special tag

Posted: Tue May 12, 2009 5:55 pm
by pickle
A simple regex for extracting the number is:

Code: Select all

/{img=(\d*)}/
Another regex you could use in preg_replace, which matches everything but the number is:

Code: Select all

/({img=)\d*?(})/
That could be broken up & used like this (untested):

Code: Select all

# first pattern matches everything before the number, second matches everything after the number
$pattern = array('/({img=)\d*?}/','/{img=\d*?(})/');
$all_done = preg_replace($pattern,array('<img src="image.php?id=','" />'),$original_string);

Re: Simple RegEx To find a number in a special tag

Posted: Wed May 13, 2009 3:49 am
by prometheuzz
... or a bit of "look-arounds" will do as well:

Code: Select all

$text = 'there are 2 things like {img=123456} or {img=9} I need';
preg_match_all('/(?<={img=)\d+(?=})/i', $text, $matches);
print_r($matches);
(notice that the number '2' is ignored)

Re: Simple RegEx To find a number in a special tag

Posted: Wed May 13, 2009 6:32 am
by socket1
That works to some extent. But it does not get the second number.
http://ajarsource.org/scripts/regex.php ... %7D%29%2Fi

Re: Simple RegEx To find a number in a special tag

Posted: Wed May 13, 2009 6:34 am
by prometheuzz
socket1 wrote:That works to some extent. But it does not get the second number.
http://ajarsource.org/scripts/regex.php ... %7D%29%2Fi
It works to a full extend IMO ;)
You're probably using preg_match(...) instead of the suggested preg_match_all(...).

Re: Simple RegEx To find a number in a special tag

Posted: Thu May 14, 2009 3:30 pm
by socket1
Thanks, it works great.
How could I modify it to make it recognize text after the {img= part ?

EDIT: I beleive I figured it out its this /(?<={img=)\D+(?=})/i to get it to recognize text.

Re: Simple RegEx To find a number in a special tag

Posted: Fri May 15, 2009 12:52 am
by prometheuzz
socket1 wrote:...

EDIT: I beleive I figured it out its this /(?<={img=)\D+(?=})/i to get it to recognize text.
That way, your text can't contain any numbers. You could do it like this:

Code: Select all

/(?<={img=)[^{}]*(?=})/
assuming the text will not contain '{' and '}'.

Re: Simple RegEx To find a number in a special tag

Posted: Fri May 15, 2009 1:10 pm
by socket1
Ah, thanks.