Problem with Regular Expression

4
preg_match('<[ \w"=]+name="xhpc_composerid"[ \w="]+>',$exec,$result1);
echo $result1[0];
//input type="hidden" autocomplete="off" name="xhpc_composerid" value="u_0_1k" 

preg_match('/value="[\w]+"/i',$result1[0],$result2);
echo $result2[0];
// value="u_0_1k"

$result3 = preg_replace('/value="/i',"ab123",$result2[0]);
echo $result3[0];
// a

$result4 = substr($result3[0],0,-1);
echo $result4;
// (vazio)

The question is $result2 printing value="u_0_1k" , why $result3 print a ? My regex should be correct, I expected something like: u_0_1k" , then in $result4 would print what I printed in $result3 only without double quotation marks, what can I be wrong?

    
asked by anonymous 13.03.2015 / 02:46

1 answer

1

I did not fully understand what was being asked. Below is an example:

  • extracting some parts (xml attributes)
  • replacing a value with a new value.

Based on your example

<?php

$exemplo='<xxx "type="hidden" autocomplete="off" name="xhpc_composerid" value="u_0_1k">';

preg_match('<.*? name="(.*?)".*? value="(.*?)".*?>',$exemplo,$result);
echo "id    encontrado foi...  $result[1]\n";
echo "valor encontrado foi...  $result[2]\n";

$r = preg_replace('/(value=")(.*?)"/',"$1Novo valor", $exemplo);
echo "$r\n";

?>

running ( php file ) produces:

id    encontrado foi...  xhpc_composerid
valor encontrado foi...  u_0_1k
<xxx "type="hidden" autocomplete="off" name="xhpc_composerid" value="Novo valor>
    
13.03.2015 / 13:38