You are here

Get substring between two strings PHP

In this example we are taken the url. The goal is to get the string between 'num-' and '/': 5555555.

http://example.com/content/view/onenodeexample---addtional-data--num-555...

$url = check_plain(request_uri());
preg_match('~m-(.*?)/~', $url, $output);
return $output[1];

Note:

$curr_uri = check_plain(request_uri());

Note that you can get the Drupal URI without using the check_plain method, like this:

$curr_uri = request_uri();

but the fact is that the check_plain method makes sure the URI is properly encoded. This is good from a security perspective.

code type: