Regex to match text twice

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
DoA
Forum Newbie
Posts: 7
Joined: Thu Jul 28, 2005 9:11 am

Regex to match text twice

Post by DoA »

Hi guys,

I need a regex expression that will ensure the the following they're both entered into the data.

[hide] and [/hide]

as well as [HIDE] and [/HIDE]

or any combination of the cases ... Regex is case sensitive isn't it?

For example, if the [hide] tag is present but a [/hide] isn't then that isn't suitable. Likewise the other way round is unsuitable, both tags must be present...

Thanks for your help!
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex to match text twice

Post by prometheuzz »

Give this a go:

Code: Select all

<?php
$tests = array(
    '... [hide] and [/hide] ...',
    'as well as [HIDE] and [/HIDE] xyz',
    'and [hide] and [hide] abc',
    'or [hide] and [/HIDE] ...',
);
foreach($tests as $t) {
    if(preg_match('#\[(hide|HIDE)\].*?\[/\1\]#', $t)) {
        echo "OK: $t\n";
    } else {
        echo "WRONG: $t\n";
    }
}
?>
HTH
DoA
Forum Newbie
Posts: 7
Joined: Thu Jul 28, 2005 9:11 am

Re: Regex to match text twice

Post by DoA »

Thanks for the super fast reply.

I am trying to combine two things. What I want to do is this;

Firstly see if I even need to check for the hide tags (certain parts of my board don't require them). Therefore I have listed the forumid's that do need the tags in an array.

If the forumid is in the array (therefore it requires hide tags) then I run the regex.... otherwise it doesn't matter do I do nothing.

Array Code

Code: Select all

<?php
$fid_array[0] = "13";
$fid_array[1] = "14";
$fid_array[2] = "15";
$fid_array[3] = "20";
$fid_array[4] = "26";
$fid_array[6] = "27";
$fid_array[7] = "28";
$fid_array[8] = "29";
$fid_array[9] = "30";
$fid_array[10] = "31";
$fid_array[11] = "32";
$fid_array[12] = "33";
$fid_array[13] = "34";
$fid_array[14] = "35";
$fid_array[15] = "36";
$fid_array[16] = "37";
$fid_array[17] = "38";
$fid_array[18] = "39";
$fid_array[19] = "40";
$fid_array[20] = "41";
$fid_array[21] = "42";
$fid_array[22] = "43";
$fid_array[23] = "44";
$fid_array[24] = "45";
$fid_array[25] = "46";
$fid_array[26] = "47";
 
if(in_array($forumid, $fid_array)) {
// TEST REGEX FOR HIDE TAGS
}
else{
// DO NOTHING
}
?>
Combined Code ??

Code: Select all

<?php
 
$fid_array[0] = "13";
$fid_array[1] = "14";
$fid_array[2] = "15";
$fid_array[3] = "20";
$fid_array[4] = "26";
$fid_array[6] = "27";
$fid_array[7] = "28";
$fid_array[8] = "29";
$fid_array[9] = "30";
$fid_array[10] = "31";
$fid_array[11] = "32";
$fid_array[12] = "33";
$fid_array[13] = "34";
$fid_array[14] = "35";
$fid_array[15] = "36";
$fid_array[16] = "37";
$fid_array[17] = "38";
$fid_array[18] = "39";
$fid_array[19] = "40";
$fid_array[20] = "41";
$fid_array[21] = "42";
$fid_array[22] = "43";
$fid_array[23] = "44";
$fid_array[24] = "45";
$fid_array[25] = "46";
$fid_array[26] = "47";
 
if(in_array($forumid, $fid_array)) {
$tests = array(
'... [hide] and [/hide] ...',
'as well as [HIDE] and [/HIDE] xyz',
'and [hide] and [hide] abc',
'or [hide] and [/HIDE] ...',);
foreach($tests as $t) {
if(preg_match('#\[(hide|HIDE)\].*?\[/\1\]#', $t)) {
// BOTH HIDE TAGS PRESENT, THAT'S OK PERFORM ACTION
} else {
// ONE OR BOTH TAGS ARE MISSING, PERFORM ERROR ACTION
}
}
}
?>
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex to match text twice

Post by prometheuzz »

I'm not sure what your question is. I mean, you shouldn't be asking me if your code is ok: you should test whether or not it's ok or not. If it's not, you can ask a specific question here on the forum and explain what's wrong with it.

Good luck!
DoA
Forum Newbie
Posts: 7
Joined: Thu Jul 28, 2005 9:11 am

Re: Regex to match text twice

Post by DoA »

Code: Select all

 
<?php
 
$forumid = 13;
$fid_array[0] = "13";
$fid_array[1] = "14";
$fid_array[2] = "15";
$fid_array[3] = "20";
$fid_array[4] = "26";
$fid_array[6] = "27";
$fid_array[7] = "28";
$fid_array[8] = "29";
$fid_array[9] = "30";
$fid_array[10] = "31";
$fid_array[11] = "32";
$fid_array[12] = "33";
$fid_array[13] = "34";
$fid_array[14] = "35";
$fid_array[15] = "36";
$fid_array[16] = "37";
$fid_array[17] = "38";
$fid_array[18] = "39";
$fid_array[19] = "40";
$fid_array[20] = "41";
$fid_array[21] = "42";
$fid_array[22] = "43";
$fid_array[23] = "44";
$fid_array[24] = "45";
$fid_array[25] = "46";
$fid_array[26] = "47";
 
 
// If forum is in array run script
 
if(in_array($forumid, $fid_array)) {
$tests = array(
'... [hide] and [/hide] ...',
'as well as [HIDE] and [/HIDE] xyz',
'and [hide] and [hide] abc',
'or [hide] and [/HIDE] ...',);
foreach($tests as $t) {
if(preg_match('#\[(hide|HIDE)\].*?\[/\1\]#', $t)) {
echo "Both hide tags there, run script"; // DO NOTHING
} else {
echo "Hide tags aren't there"; // hide tags missing, run error procedure
}
}
}
?>
 
This is giving the following

Code: Select all

Both hide tags there, run scriptBoth hide tags there, run scriptHide tags aren't thereHide tags aren't there
I don't understand because I haven't plugged it into the script therefore the hide tags def aren't there yet.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex to match text twice

Post by prometheuzz »

I have no idea what you're talking about!
You realise that the array in my example is just a couple of test cases, right?
Post Reply