dlate Posted August 10, 2008 Share Posted August 10, 2008 Hi, (this is a PCRE related issue) This has probably been asked already, but i did a search and couldnt find my anwser, so here goes: Im trying to preg_match and then preg_replace between two tags. The html would look like this: <dmod::user|overview> <drow>asdasdasd</drow> </dmod> For the preg_match i tried this but it failed and im pretty clueless on how to get it to work: if(preg_match('/(<dmod:(.+?)(>)(.+?)(</dmod>)/iU', $str)) { echo 'match?'; } If u guys got any tips on how to fix this, or point me in the right direction id really appreciate it. Thanks, Dlate Quote Link to comment https://forums.phpfreaks.com/topic/119043-regex-get-text-from-within-two-tags/ Share on other sites More sharing options...
dlate Posted August 10, 2008 Author Share Posted August 10, 2008 update: This should be the right pattern but its still not working... going crazy... /<dmod([^>].*[^/])>/im Quote Link to comment https://forums.phpfreaks.com/topic/119043-regex-get-text-from-within-two-tags/#findComment-613059 Share on other sites More sharing options...
dlate Posted August 10, 2008 Author Share Posted August 10, 2008 Update 2: I solved it! problem was i wasnt escaping the pattern proper so my modifiers would get messed up. correct pattern was : if(preg_match('/(<dmod:(.+?)(>)(.+?)(<\/dmod>)/iU', $str)) { echo 'match?'; } Hopes this might help other people with similiar problems Quote Link to comment https://forums.phpfreaks.com/topic/119043-regex-get-text-from-within-two-tags/#findComment-613065 Share on other sites More sharing options...
dlate Posted August 10, 2008 Author Share Posted August 10, 2008 Ok apprently this doesnt work over several lines and im completly clueless again ??? Quote Link to comment https://forums.phpfreaks.com/topic/119043-regex-get-text-from-within-two-tags/#findComment-613083 Share on other sites More sharing options...
dlate Posted August 10, 2008 Author Share Posted August 10, 2008 Solved it! I missed the /s for spaces, i really hope someone in the future can get help from this topic tho, took me a while to figure it out. final code: $pattern = '/<dmod:.+?)>(.+?)<\/dmod>/is'; if(preg_match($pattern, trim($output), $matches)) { $output = preg_replace($pattern, $matches[2], $output); } Quote Link to comment https://forums.phpfreaks.com/topic/119043-regex-get-text-from-within-two-tags/#findComment-613096 Share on other sites More sharing options...
effigy Posted August 11, 2008 Share Posted August 11, 2008 - By changing the delimiter, there's no need to escape /. - Greediness is generally more efficient than laziness; use [^>]+. - Why run a match and a replace? <pre> <?php $output = <<<DATA <dmod::user|overview> <drow>asdasdasd</drow> </dmod> DATA; echo htmlspecialchars($output), '<hr>'; $pattern = '%<dmod:[^>]+)>(.+?)</dmod>%is'; $output = preg_replace($pattern, '$2', $output); echo htmlspecialchars($output); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/119043-regex-get-text-from-within-two-tags/#findComment-613550 Share on other sites More sharing options...
dlate Posted August 11, 2008 Author Share Posted August 11, 2008 Hehe nice, didnt know u could change the delimiter As far as me using preg_match as well as replace is because i want to save the data between the tags and then replace it. Thanks for ur input, i really appreciate any help i can get with regex as im a complete newbie with it, could you please explain why ur putting <<<DATA in ur string? Quote Link to comment https://forums.phpfreaks.com/topic/119043-regex-get-text-from-within-two-tags/#findComment-613750 Share on other sites More sharing options...
effigy Posted August 11, 2008 Share Posted August 11, 2008 preg_match only matches, but preg_replace matches and replaces (what it matches). Essentially, what you have is... if (my string matches the pattern) { run the replace against the pattern } ...which is pointless because you cannot replace what you cannot match--all you need is the "run the replace against the pattern" step. <<< is heredoc syntax. Quote Link to comment https://forums.phpfreaks.com/topic/119043-regex-get-text-from-within-two-tags/#findComment-613765 Share on other sites More sharing options...
dlate Posted August 11, 2008 Author Share Posted August 11, 2008 Ah i see, thank you very much u have been very helpfull. When im done with this script ill probably have to run trough it a couple of times to make sure i remove any redundant code Quote Link to comment https://forums.phpfreaks.com/topic/119043-regex-get-text-from-within-two-tags/#findComment-613775 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.