Simple RegEx To find a number in a special tag
Moderator: General Moderators
Simple RegEx To find a number in a special tag
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.
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
A simple regex for extracting the number is:
Another regex you could use in preg_replace, which matches everything but the number is:
That could be broken up & used like this (untested):
Code: Select all
/{img=(\d*)}/Code: Select all
/({img=)\d*?(})/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.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Simple RegEx To find a number in a special tag
... or a bit of "look-arounds" will do as well:
(notice that the number '2' is ignored)
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);Re: Simple RegEx To find a number in a special tag
That works to some extent. But it does not get the second number.
http://ajarsource.org/scripts/regex.php ... %7D%29%2Fi
http://ajarsource.org/scripts/regex.php ... %7D%29%2Fi
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Simple RegEx To find a number in a special tag
It works to a full extend IMOsocket1 wrote:That works to some extent. But it does not get the second number.
http://ajarsource.org/scripts/regex.php ... %7D%29%2Fi
You're probably using preg_match(...) instead of the suggested preg_match_all(...).
Re: Simple RegEx To find a number in a special tag
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.
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.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Simple RegEx To find a number in a special tag
That way, your text can't contain any numbers. You could do it like this:socket1 wrote:...
EDIT: I beleive I figured it out its this /(?<={img=)\D+(?=})/i to get it to recognize text.
Code: Select all
/(?<={img=)[^{}]*(?=})/