-2

i have string on the variable and question is how find any character after search/node/ and print that ?

String:

$var="search/node/Apartman";

my Codes :

$var="search/node/Apartment";    //URL

preg_match('/search/node/\K\d+/', $var,$newvar);

$FinalVAR=$newvar[0];

print_r ($FinalVAR);

BUT NOT WORK

THANKS FOR ANY HELP.

Zare
  • 23
  • 4

1 Answers1

0

When using / as the delimiter, / in the pattern should be escaped. or use other rarely used char as delimiter, such as ~. The working code:

$var="search/node/Apartman";

preg_match('/search\/node\/(.*)/', $var,$newvar);

var_export($newvar);

Output

array (
  0 => 'search/node/Apartman',
  1 => 'Apartman',
)
ild flue
  • 1,323
  • 8
  • 9