Dead6re Posted October 29, 2008 Share Posted October 29, 2008 Hello, I'm having a problem trying to match pipe characters if there is more than one: e.g. {hyperlink|ahref|text} Using: $this->contents = preg_replace_callback('/\{[A-z0-9\-\|,\.\/]+\}/', array($Template, 'replace'), $this->contents); Quote Link to comment https://forums.phpfreaks.com/topic/130598-only-matching-a-pipe-character-once/ Share on other sites More sharing options...
ddrudik Posted October 29, 2008 Share Posted October 29, 2008 \|(?=[^{}]*}) Although I suspect your question is a different one than that. $this->contents = preg_replace_callback('/\{([^}]*)\}/', array($Template, 'replace'), $this->contents); then in replace($match): $matches=explode('|',$match[1]); loop through each $matches to do your replacements. Quote Link to comment https://forums.phpfreaks.com/topic/130598-only-matching-a-pipe-character-once/#findComment-677563 Share on other sites More sharing options...
ghostdog74 Posted October 29, 2008 Share Posted October 29, 2008 Hello, I'm having a problem trying to match pipe characters if there is more than one: $string = '{hyperlink|ahref|text}'; if ( substr_count($string,"|") > 1 ) { echo "yes, more than 1\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/130598-only-matching-a-pipe-character-once/#findComment-677569 Share on other sites More sharing options...
Dead6re Posted October 29, 2008 Author Share Posted October 29, 2008 Okay, I think I need to explain a bit more. I have a template class in PHP that should replace values within {} brackets. Inside those brackets, the characters A-z, 0-9, Hyphen (-), Pipe (|), Comma (,), Full-stop (.) & Forward Slash (/). Using the expression tester here using my expression it works: http://www.gskinner.com/RegExr/ However using it in PHP does not. For some reason matching {} brackets from my file does not work. It seems that having a range of A-z 0-9 matches more than one character However matching a special character seems to match it once and not more. e.g. It should match all expressions within the {} brackets below [in navy] (Simple HTML) <html> <head> <title>{title}</title> {css|style.css,layout.css} </head> <body> {hyperlink|ahref|text}{randomtext} </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/130598-only-matching-a-pipe-character-once/#findComment-677621 Share on other sites More sharing options...
ddrudik Posted October 29, 2008 Share Posted October 29, 2008 \{[^}]+\} Quote Link to comment https://forums.phpfreaks.com/topic/130598-only-matching-a-pipe-character-once/#findComment-677637 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.