Pratik Jaiswal
Pratik Jaiswal

Reputation: 299

PHP print only some part of the string

I have few PHP strings ($url1, $url2, ..) as below:

$url1 = "http://build:[email protected]:8080/job/new-ios-2.1/buildWithParameters";

$url2 = "http://build:[email protected]:8080/job/new-android-8.2/buildWithParameters";

How do I get the substring after /job/ and before /buildWithParameters? Here is the expected output for $url1:

new-ios-2.1

So far I have tried using substr function:

For example, $url1 = substr($url, -10);. I am not able to find the above desired job part by this approach. Is there a better way to do this in PHP?

Upvotes: 0

Views: 596

Answers (2)

Arup Garai
Arup Garai

Reputation: 151

You Can use explode function . 4th key's value will be as per your expectation

$url1 = "http://build:[email protected]:8080/job/new-ios-2.1/buildWithParameters";
$urlarr=explode('/', $url1);
print_r($urlarr);

Upvotes: 1

Ahmad Hajjar
Ahmad Hajjar

Reputation: 1853

Use this regex:

  preg_match('/\/job\/([^\/]*)\/buildWithParameters/', $url1, $matches);
  print_r($matches[1]);

This will match the string between first occurrence of /job/ and first occurence of /buildWithParameters/

Check the demo here

Upvotes: 1

Related Questions