glenelkins Posted May 21, 2009 Share Posted May 21, 2009 Hi My strongest point is just not REGEX! Im trying to return a string that is between {sls}Content to return here{/sle} So basically {sls} must be at the beginning, and {/sle} at the end and return the "Content to return here" bit how would i do this? Iv tried: preg_match ( '/^({sls})({/sle})$/', $string, $array ); but get the following error: Warning: preg_match() [function.preg-match]: Unknown modifier 'l' in /home/clients/public_html/cms/application/Engine/plugins/function.admin_list_sections.php on line 26 cheers Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted May 21, 2009 Share Posted May 21, 2009 I cant tell if that is a pipe or an l between ss and se. If not <?php $subject = "{sls}Content to return here{/sle}"; if(preg_match('%{sls}(.*){/sle}%', $subject, $regs)) { print $regs[0]; } ?> If it is a pipe <?php $subject = "{s|s}Content to return here{/s|e}"; if(preg_match('%{s\\|s}(.*){/s\\|e}%', $subject, $regs)) { print $regs[0]; } ?> Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted May 21, 2009 Share Posted May 21, 2009 Get a copy of regex buddy to help you with regex http://www.regexbuddy.com/ Quote Link to comment Share on other sites More sharing options...
glenelkins Posted May 21, 2009 Author Share Posted May 21, 2009 its an L not a pipe Iv tried {sls}(.*}{/sle} and the array comes back empty! what are the % for? Quote Link to comment Share on other sites More sharing options...
glenelkins Posted May 21, 2009 Author Share Posted May 21, 2009 oh it does work. thopugh only if its text. i want it to do this on HTML code but when i use HTML as the subject like the exmaple below, the array of items is empty: <table> {sle} <tr> <td>TEST</td> </tr> {/sle} </table> Quote Link to comment Share on other sites More sharing options...
glenelkins Posted May 21, 2009 Author Share Posted May 21, 2009 so why doesnt it work when the content is HTML? Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted May 21, 2009 Share Posted May 21, 2009 Thats because it contains line break characters. Ammend the regex to if(preg_match('%{sls}(.*){/sle}%s', $subject, $regs)) { Quote Link to comment Share on other sites More sharing options...
Axeia Posted May 21, 2009 Share Posted May 21, 2009 Iv tried {sls}(.*}{/sle} and the array comes back empty! That's not a valid regex, you tried to close a ( with a }? Close the ( with a ) what are the % for? You need to use the same (unique) character for the first and last place, % is unlikely to be used in the rest of the regex. Quote Link to comment Share on other sites More sharing options...
glenelkins Posted May 21, 2009 Author Share Posted May 21, 2009 can you tell me what the % and %s are for i thought with preg_match you have to start and end the expression with / Quote Link to comment Share on other sites More sharing options...
glenelkins Posted May 21, 2009 Author Share Posted May 21, 2009 works now! thanks guys Quote Link to comment Share on other sites More sharing options...
glenelkins Posted May 21, 2009 Author Share Posted May 21, 2009 ok another quick one once i have the text between the tags, and done what i need to with it how would i then go back and replace the old text between those tags with the new text? Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted May 21, 2009 Share Posted May 21, 2009 That would be preg_replace() Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 can you tell me what the % and %s are for i thought with preg_match you have to start and end the expression with / They're delimiters. It just means it holds a regex expression. Without them, it would match just a string, but if you want to match strings, go with strpos or strstr. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted May 21, 2009 Share Posted May 21, 2009 if(preg_match('%{sls}(.*){/sle}%s', $subject, $regs)) { In general, it is frowned upon to use .* or .+ This thread explains why (post #11 and 14 - In other words, potential speed / accuracy issues). So I would make it a lazy quantifier... can you tell me what the % and %s are for i thought with preg_match you have to start and end the expression with / They're delimiters. It just means it holds a regex expression. Without them, it would match just a string, but if you want to match strings, go with strpos or strstr. Actually, without delimiters, you would get warnings (unless it just so happens that the first and last character within the quoted pattern (modifiers aside) is a viable delimiter / delimiter set - in which case the regex engine would then treat as delimiters), which in turn might not generate an error or warning, but you won't get the results you are looking for. A typical pitfall example: preg_match('<img src="([^"]+)" />', $str, $match); This is legal (only because the first and last character form a legit delimiter set.. so the delimiters are < and >, thus the pattern only looks for img src=... when the person in question might want to include the < and > characters. Without any form of delimiters, you'd get warnings. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted May 21, 2009 Share Posted May 21, 2009 Was just a quick example if(preg_match('%{sls}(.*){/sle}%s', $subject, $regs)) { How do you suggest rewriting to match absolutely anything inside the capture group? Quote Link to comment Share on other sites More sharing options...
Zhadus Posted May 21, 2009 Share Posted May 21, 2009 $newcode = preg_replace('%{sls}(.*){/sle}%s', $replacewith, $input); Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted May 21, 2009 Share Posted May 21, 2009 Was just a quick example if(preg_match('%{sls}(.*){/sle}%s', $subject, $regs)) { How do you suggest rewriting to match absolutely anything inside the capture group? That doesn't sound quite right... everything the dot_match_all can get will be stored into the capture.. By this you mean matching only between {sls} and {/sle}? By making it lazy through the addition of the ? after .* if(preg_match('%{sls}(.*?){/sle}%s', $subject, $regs)) { Quote Link to comment 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.