Simple RegEx To find a number in a special tag

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Simple RegEx To find a number in a special tag

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

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

Post 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)
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

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

Post by socket1 »

That works to some extent. But it does not get the second number.
http://ajarsource.org/scripts/regex.php ... %7D%29%2Fi
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

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

Post 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(...).
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

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

Post 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.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

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

Post 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 '}'.
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

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

Post by socket1 »

Ah, thanks.
Post Reply