Regex in dynamic string X characters, in dynamic content

Asked

Viewed 367 times

4

Next, I have a page that generates a shuffled content with dynamic and non-dynamic strings, I need to take a dynamic value between separators |, it contains random data/strings around, which change when loading the page. The variable I want to pick has a fixed number of 32 characters composed of letters, numbers and _ (a-zA-Z0-9_).

Example:

PAGE CONTENTS

...new "x";test>'ok'=blabla-bla;||inicio||var77|var2|44AsFGq72A7_7Aq770vAr45|variavel|randomvariavel75|87df".fim("0"...

I wish to take the value of 33 characters (44AsFGq72A7_7Aq770vAr45) but I don’t know how because when the page is updated the values of the Lada of the 23 characters change order, and the string itself also because it is random.

Example page update.

(PS: The start and end values are always in the same place)

...new "x";test>'ok'=blaalb-bla;||inicio||r_a_n_d99|var2|84DF8|7s79DFGDsf8ssfs84D84d8D|var77|8526".fim("0"...

I need to use regex in the php to take this random variable on this random page excluding any other code that may appear.

Is there any way to do that?

  • 2

    The string you want has 23.32 or 33 characters?

  • I’m sorry for the inconsistency, they’re actually more than one, I ended up mixing, but only one example fits the other, let’s say you’re 23.

2 answers

3


$str = 'new "x";test>\'ok\'=blabla-bla;||inicio||var77|var2|44AsFGq72A7_7Aq770vAr45|variavel|randomvariavel75|87df".fim("0"...';
preg_match_all( '/\|[a-zA-Z0-9_]{23}\|/' , $str , $out );
print_r( $out );

You can use {N} to set the length of the string you want to find. In the above example you will find the 23-character string containing a-zA-Z0-9_ that is within the bounder |.

Output: 44AsFGq72A7_7Aq770vAr45

Example in Ideone

  • 1

    It solved perfectly, thanks for the time dedicated, I only had to use one str_replace to remove a static value and is perfect, thank you.

3

As @Papacharlie commented you can use {N} to delimit the exact number of characters of wish.

As for regex:

You can use the auxiliary \w which it rightly represents a-zA-Z0-9_

preg_match( '~(?<=\|)(\w{23})(?=\|)~' , $str , $out );

(?<=\|) auxiliary - should contain before, but not capture
(?=\|) auxiliary - must contain afterwards, but not capture

or simply

preg_match( '~\|(\w{23})\|~' , $str , $out );
$var = $out[1]; the value will be in group 1;

  • 1

    Without a doubt they are valuable information, I learned a little more, they will be of great help in the future.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.