jamesxg1 Posted March 23, 2010 Share Posted March 23, 2010 Hiya! I was wondering how I would search for tags in a file. I have this. $file = file_get_contents('global.html'); I was wondering how I would search for all things like this, {TAGNAME} and I can add the TAGNAME into an array? Many thanks James. Link to comment https://forums.phpfreaks.com/topic/196254-how-do-i-make-a-script-search-for-tags/ Share on other sites More sharing options...
Pawn Posted March 23, 2010 Share Posted March 23, 2010 You could use preg_match. Something like: preg_match("/\{([A-Z]*)\}/",$file,$match) unset($match[0]); foreach($match as $key => $val) { echo "Match ".$key." = ".$val; } Disclaimer: I cannot write regex to save my life. Link to comment https://forums.phpfreaks.com/topic/196254-how-do-i-make-a-script-search-for-tags/#findComment-1030617 Share on other sites More sharing options...
jamesxg1 Posted March 23, 2010 Author Share Posted March 23, 2010 You could use preg_match. Something like: preg_match("/\{([A-Z]*)\}/",$file,$match) unset($match[0]); foreach($match as $key => $val) { echo "Match ".$key." = ".$val; } Disclaimer: I cannot write regex to save my life. Hiya mate, Cheers for the reply, that works pretty well except if I have {ONE} {TWO} {THREE} it will only return Match 1 = ONE Many thanks James. Link to comment https://forums.phpfreaks.com/topic/196254-how-do-i-make-a-script-search-for-tags/#findComment-1030621 Share on other sites More sharing options...
Pawn Posted March 23, 2010 Share Posted March 23, 2010 Oops! <? $file = "{ONE} {TWO} {THREE}"; preg_match_all("/\{([A-Z]*)\}/",$file,$match); foreach($match[1] as $key => $val) { echo "Match ".$key." = ".$val."<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/196254-how-do-i-make-a-script-search-for-tags/#findComment-1030662 Share on other sites More sharing options...
jamesxg1 Posted March 23, 2010 Author Share Posted March 23, 2010 Oops! <? $file = "{ONE} {TWO} {THREE}"; preg_match_all("/\{([A-Z]*)\}/",$file,$match); foreach($match[1] as $key => $val) { echo "Match ".$key." = ".$val."<br />"; } ?> Worked like a star cheers mat much appreciated. Link to comment https://forums.phpfreaks.com/topic/196254-how-do-i-make-a-script-search-for-tags/#findComment-1030663 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.