Page 1 of 1
regex help....
Posted: Thu Jul 02, 2009 2:19 am
by revbackup
I need a regex to put into an array all the text that starts with R1Y2A3N and ends with R1Y2A3N.....
Example, I have a variable like this:
$variable = 'Hello R1Y2A3Ne2p0YWc6cGFyYW0gbmFtZT12ZW5kb3JOYW1lfQ==R1Y2A3N and you!';
so, R1Y2A3Ne2p0YWc6cGFyYW0gbmFtZT12ZW5kb3JOYW1lfQ==R1Y2A3N should be put into an array....
please help me with this... thanks!
Re: regex help....
Posted: Thu Jul 02, 2009 2:31 am
by requinix
Base 64 encoding is sooo easy to recognize
Code: Select all
$substr = "R1Y2A3N";
$string = "Hello R1Y2A3Ne2p0YWc6cGFyYW0gbmFtZT12ZW5kb3JOYW1lfQ==R1Y2A3N and you!";
// make sure that $substr shows up (at least) twice in $string
if (strpos($string, $substr, strpos($string, $substr) + 1)) {
list($left, $inner, $right) = explode($substr, $string);
echo $substr . $inner . $substr, "<br>\n";
echo base64_decode($inner), "<br>\n";
}
Re: regex help....
Posted: Thu Jul 02, 2009 2:41 am
by revbackup
is preg_match_all possible to do this?
I have this code,
preg_match_all("/(?s)R1Y2A3N(.+?)\R1Y2A3N/", $variable, $matches, PREG_PATTERN_ORDER);
but it would not work properly... i think this needs some modification and I dont know what to modify....
i want it to be in preg_match_all because the matches are stored in an array and i can simply
do a foreach in each matches....
Re: regex help....
Posted: Thu Jul 02, 2009 3:26 am
by requinix
Is there going to be more than one match?
Regular expressions are slow. Using normal string functions (if possible) is best.
Re: regex help....
Posted: Thu Jul 02, 2009 4:14 am
by BornForCode
In the case that you have more then a result go for regex.
Re: regex help....
Posted: Thu Jul 02, 2009 5:21 am
by prometheuzz
revbackup wrote:is preg_match_all possible to do this?
I have this code,
preg_match_all("/(?s)R1Y2A3N(.+?)\R1Y2A3N/", $variable, $matches, PREG_PATTERN_ORDER);
but it would not work properly... i think this needs some modification and I dont know what to modify....
i want it to be in preg_match_all because the matches are stored in an array and i can simply
do a foreach in each matches....
Remove the backslash from your regex pattern.
And you can remove the PREG_PATTERN_ORDER, by default this option is already chosen.