Jump to content

preg_match question


glenelkins

Recommended Posts

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

 

Link to comment
Share on other sites

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];
}
?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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)) {

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.