Jump to content

How do I make a script search for tags.


jamesxg1

Recommended Posts

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

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.