0

I have few string sequences.

<fn>Explode(<ltr>|</ltr>,<fld>SALTEST.ANUNCIO.ID_CIUDAD</fld>,<var>$P[1]</var>)</fn>

<fld>SALTEST.ANUNCIO.ESTADO </fld><fld>SALTEST.ANUNCIO.MUNICIPIO </fld><fld>SALTEST.ANUNCIO.CIUDAD</fld>

 <fld>SALTEST.ANUNCIO.ESTADO</fld><ltr>,</ltr> <fld>SALTEST.ANUNCIO.MUNICIPIO</fld><ltr>,</ltr><fld>SALTEST.ANUNCIO.CIUDAD</fld>

I am sending this to server , Its like an instruction to server which represent following instruction

For the first one

  explode("|",SALTEST.ANUNCIO>ID_CIUDAD,$p[1]);

How can I do this .

Thanks in advance

Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130

2 Answers2

1

I would avoid passing in dynamic variables to create code...

But... you could always do this:

$sOutputString = str_replace( array( '<fn>', '</fn>', '<ltr>', '</ltr>', '<fld>', '</fld>', '<var>', '</var>' ), '', $sInString );

For more efficiency:

$sJson = '{"fn":"Explode","ltr":"|","fld":"SALTEST.ANUNCIO.ID_CIUDAD","var":"$P[1]"}';

$aJson[ 'fn' ] = 'Explode';
$aJson[ 'ltr' ] = '|';
$aJson[ 'fld' ] = 'SALTEST.ANUNCIO.ID_CIUDAD';
$aJson[ 'var' ] = '$P[1]';
$sJson = json_encode( $aJson );
var_dump( $sJson );

$aJson = json_decode( $sJson, 1 );
var_dump( $aJson );
Vladimir Ramik
  • 1,920
  • 2
  • 13
  • 23
1

Use preg_match()

<?php
$P[1] = 5;
$i = "<fn>explode(<ltr>|</ltr>,<fld>SALTEST.ANUNCIO.ID_CIUDAD</fld>,<var>$P[1]</var>)</fn>";
preg_match("!<fn>(.*)</fn>!",$i,$fn);
//Matches everything in <fn> block
//$fn[0] contains whole string and $fn[1] contains matched text.

preg_match("!<ltr>(.*)</ltr>!",$fn[1],$ltr);
preg_match("!<fld>(.*)</fld>!",$fn[1],$fld);
preg_match("!<var>(.*)</var>!",$fn[1],$var);
preg_match("!(.*)\(!",$fn[1],$cmd);


$cmd= $cmd[1];
$var=$var[1]+0;
$fld=$fld[1];
$ltr=$ltr[1];
//Execute
$cmd($ltr,$fld,$var);
?> 

Note:

1)I've changed Export to export in input as there's no function like Export in PHP.

2) I've initialized $P[1] but if you want it to be a raw array, then first match $P[1] from the input then using regex !(.*)[! Match the array name, here P. Again use regex and match array index and then extract the required array and then set it to be $var.

Output: http://ideone.com/LNC52n

Hritik
  • 673
  • 1
  • 6
  • 24