regex help....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
revbackup
Forum Commoner
Posts: 29
Joined: Tue Jun 09, 2009 1:52 am

regex help....

Post 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!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: regex help....

Post by requinix »

Base 64 encoding is sooo easy to recognize :o

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";
}
revbackup
Forum Commoner
Posts: 29
Joined: Tue Jun 09, 2009 1:52 am

Re: regex help....

Post 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....
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: regex help....

Post by requinix »

Is there going to be more than one match?

Regular expressions are slow. Using normal string functions (if possible) is best.
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: regex help....

Post by BornForCode »

In the case that you have more then a result go for regex.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: regex help....

Post 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.
Post Reply