sparkle
sparkle

Reputation: 7390

How to get part of string with Regular expression?

this is my string: http://www.something.com/path/tt0425235/somethingelse/

and i need to get just "0425235" (Beetween "tt" and "/")

Test with:http://rubular.com/

Upvotes: 1

Views: 97

Answers (4)

Olaf Erlandsen
Olaf Erlandsen

Reputation: 6036

Simple and perfect ;)

<?php
$match = array();
$url = 'http://www.something.com/path/tt0425235/somethingelse/bla/bla/bla/bla/?bla=bla';
preg_match( '/\/path\/([a-z]+([\d]+))+\/?/i' , $url , $match );
print_r( $match );
?>

Bye!

Upvotes: 2

hakre
hakre

Reputation: 197659

$url = 'http://www.something.com/path/tt0425235/';

list($id) = preg_split('~^.*/tt(\d+)/$~', $url, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) + array(NULL);

If it can't find your pattern, $id will be NULL.

Upvotes: 1

ssbb
ssbb

Reputation: 2025

/http:\/\/[\w+-_\.]+\/path\/tt(\d+)\//

Or

~http://[\w+-_\.]+/path/tt(\d+)/~

Upvotes: 0

piotrekkr
piotrekkr

Reputation: 3181

#http://www\.imdb\.com/title/tt(\d+)/#

Upvotes: 2

Related Questions