Regex in dynamic string of X characters, in dynamic content

4

Next, I have a page that generates content scrambled with dynamic and non-dynamic strings, I need to get a dynamic value between the | tabs, it contains random data / strings that change when loading the page. The variable I want to get has a fixed character number of 32 consisting of letters, numbers and _ ( a-zA-Z0-9_ ).

Example :

CONTENT OF THE PAGE

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

I want to get the value of 33 characters ( 44AsFGq72A7_7Aq770vAr45 ) but I do not know how when the page is updated the values of the string of 23 characters change order, and the string itself as well random.

Example page refresh.

  

(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 on php to get this random variable on that random page, excluding any other code that might appear.

Is there any way to do this?

    
asked by anonymous 13.06.2015 / 07:44

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 example above you will find the string of 23 characters containing a-zA-Z0-9_ that is between the | delimiter.

Output: 44AsFGq72A7_7Aq770vAr45

Example on Ideone

    
13.06.2015 / 13:22
3

As @PapaCharlie commented, you can use {N} to delimit the exact number of characters you want.

As for regex:

You can use the \w helper that represents just a-zA-Z0-9_

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

% auxiliary_default - must contain before, but not capture% auxiliary_default - must contain after, but not capture

or simply

(?<=\|)
(?=\|) the value will be in group 1;

    
13.06.2015 / 16:15