Grego Posted December 23, 2007 Share Posted December 23, 2007 Essentially, I want to look for the same bit of text twice in the string. So if I had the string "<tag>Hello</tag>", it would be recognised. However, if the string were "<tag>Hello</notag>" I want it to not be recognised. Currently I'm using my extremely basic regex of: "<(.*?)>(.*?)<\/(.*?)>" but this is mixing the tags up and causing problems. How can I change it so that the first and third references (in bold) have to be the same? Thanks in advance =D Link to comment https://forums.phpfreaks.com/topic/82947-repeated-bit-of-text-possibly-quite-simple/ Share on other sites More sharing options...
dsaba Posted December 24, 2007 Share Posted December 24, 2007 give sample data and sample outputs/matches Link to comment https://forums.phpfreaks.com/topic/82947-repeated-bit-of-text-possibly-quite-simple/#findComment-422204 Share on other sites More sharing options...
dsaba Posted December 24, 2007 Share Posted December 24, 2007 Really need to see some sample data, but study/take a look at these patterns for matching duplicate xml nodes in xml data: ~(?m)^(.*+)$(?=(?:\r?\n.*+)*?\r?\n\1$)~ <a href="http://nancywalshee03.freehostia.com/regextester/regex_tester.php?seeSaved=4xm5cyfp">Seen here.</a> ~^(.*)$(?=.*\1)~ms <a href="http://nancywalshee03.freehostia.com/regextester/regex_tester.php?seeSaved=ddjcgh5m">Seen here.</a> Link to comment https://forums.phpfreaks.com/topic/82947-repeated-bit-of-text-possibly-quite-simple/#findComment-422292 Share on other sites More sharing options...
Grego Posted December 24, 2007 Author Share Posted December 24, 2007 I hope this is what you mean by sample data... Will Match: <tag>Text</tag> <tagt>Text</tagt> Won't Match: <tag>Text</tagt> <tagt>Text</tag> So I need it to look for a closing tag which is the same as the opening tag and match those together, returning the tag name (tag/tagt) and the string in between the tags (Text)... Is that what you wanted to know? If it helps, I was using <notag> as just a name. In retrospect that wasn't a very smart idea... Link to comment https://forums.phpfreaks.com/topic/82947-repeated-bit-of-text-possibly-quite-simple/#findComment-422322 Share on other sites More sharing options...
effigy Posted December 25, 2007 Share Posted December 25, 2007 <pre> <?php $data = <<<DATA Will Match: <tag>Text</tag> <tagt>Text</tagt> Won't Match: <tag>Text</tagt> <tagt>Text</tag> DATA; preg_match_all('%<([^>]+)>(.*?)</\1>%', $data, $matches); print_r($matches); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/82947-repeated-bit-of-text-possibly-quite-simple/#findComment-422954 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.