Simple RegEx To find a number in a special tag
Posted: Tue May 12, 2009 5:00 pm
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.