buzzdeep Posted October 30, 2011 Share Posted October 30, 2011 Hi PHP Experts...I'm new to regex expressions and facing a problem. $pattern = "/\[amp (.*?)\](.*?)\[\/amp\]/"; This is suppose to look for tags [amp showbox=true|otheroptions=false] some text [/amp] Now this word fine as long as I use the following text in the fronted: [amp showbox=true|otheroptions=false]Click here to download[/amp] But it breaks when I use any of the following: [amp showbox=true|otheroptions=false]<a href=http://someurl.com>Click here</a> to download[/amp] Possibly because of closing </a> as it has a forward slash. [amp showbox=true|otheroptions=false] Click here to download [/amp] Possibly because of new line returns. [amp showbox=true|otheroptions=false]<h3>Click here to download</h3>[/amp] Possibly because of closing </h3> as it has a forward slash. Could someone please help modify the regex expression to take care of all the three situations? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/250122-forward-slash-new-line-issue-urgent-pls/ Share on other sites More sharing options...
silkfire Posted October 30, 2011 Share Posted October 30, 2011 Easy. Use '%' as your regex delimiter. And add the DOTALL flag so it goes past newline (\n). $pattern = '%\[amp (.*?)\](.*?)\[\/amp\]%s' Quote Link to comment https://forums.phpfreaks.com/topic/250122-forward-slash-new-line-issue-urgent-pls/#findComment-1283540 Share on other sites More sharing options...
buzzdeep Posted October 31, 2011 Author Share Posted October 31, 2011 No, didn't work It still doesn't take </ ...new line is also not working...atelast if I can make it work for closing html tag (</) that should help me for now... Quote Link to comment https://forums.phpfreaks.com/topic/250122-forward-slash-new-line-issue-urgent-pls/#findComment-1283546 Share on other sites More sharing options...
silkfire Posted October 31, 2011 Share Posted October 31, 2011 Then you're doing something wrong. Show me your full code. Don't forget the 's' at the end of the regex. Quote Link to comment https://forums.phpfreaks.com/topic/250122-forward-slash-new-line-issue-urgent-pls/#findComment-1283548 Share on other sites More sharing options...
buzzdeep Posted October 31, 2011 Author Share Posted October 31, 2011 $pattern = '%\[amp (.*?)\](.*?)\[\/amp\]%s'; preg_match_all($pattern, $content, $matches); for ($loop1 = 0; $loop1 < sizeof($matches[1]); $loop1++) { $protected_content = $matches[2][$loop1]; $fields = explode("|", $matches[1][$loop1]); for ($loop2 = 0; $loop2 < sizeof($fields); $loop2++){ $colums = explode("=", $fields[$loop2]); for ($loop3 = 0; $loop3 < sizeof($colums); $loop3++){ switch($colums[$loop3]) { case "error": $error = trim($colums[$loop3+1]); break; case "pid": $pid = trim($colums[$loop3+1]); break; case "showbox": $showbox = trim($colums[$loop3+1]); break; default: break; } } } If I use the reg expression you gave me then doesn't enter in the for loop at all. I use this content in the front end which is passed to the function that parses using regex code above. [amp showbox=true|error=You must be logged in, <b>with an active subscription</b>, to access this file. |pid=53]<a href=http://yahoo.com>Click here to download this lesson</a>[/amp] Quote Link to comment https://forums.phpfreaks.com/topic/250122-forward-slash-new-line-issue-urgent-pls/#findComment-1283551 Share on other sites More sharing options...
buzzdeep Posted October 31, 2011 Author Share Posted October 31, 2011 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/250122-forward-slash-new-line-issue-urgent-pls/#findComment-1283769 Share on other sites More sharing options...
xyph Posted October 31, 2011 Share Posted October 31, 2011 His expression works with the example you've provided. The problem isn't in the RegEx. Quote Link to comment https://forums.phpfreaks.com/topic/250122-forward-slash-new-line-issue-urgent-pls/#findComment-1283774 Share on other sites More sharing options...
buzzdeep Posted November 2, 2011 Author Share Posted November 2, 2011 Yes, my bad..thanks. But now I'm stuck at the preg_replace part. I'm using this.. $pattern = '%\[amp (.*?)\]". $protected_content ."\[\/amp\]%s'; $form = function_login_form(); $content = preg_replace($pattern, $form, $content); ...but apparantly, preg_replace is not able to find that pattern because it doesn't replace the pattern in the content with the form. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/250122-forward-slash-new-line-issue-urgent-pls/#findComment-1284160 Share on other sites More sharing options...
xyph Posted November 2, 2011 Share Posted November 2, 2011 Without knowing what $protected_content or $form hold, it's very hard to advise. Quote Link to comment https://forums.phpfreaks.com/topic/250122-forward-slash-new-line-issue-urgent-pls/#findComment-1284321 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.