Forum Moderators: coopster

Message Too Old, No Replies

Pregmatch help

         

residual

1:46 pm on Sep 16, 2008 (gmt 0)

10+ Year Member



So I need to pregmatch a url

http://www.example.com/viewVideo/Flags_/Playstation_3/FRACTURE_the_weapons/886086

The problem is flags maybe 2 or 5. I only need the numbers at the end of the url

This is what I have for the expression so far but it is not working

$muster= '#'.$urlex[0].'(?:www\.)?example.com/[^\/]+/(\d+)(?:[^"]+)?'.$urlex.'#';

[1][edited by: eelixduppy at 3:18 pm (utc) on Sep. 16, 2008]
[edit reason] exemplified [/edit]

PHP_Chimp

4:47 pm on Sep 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you already know that what is coming in is a url? As do you actually need to preg_match, or could you just use explode and take the last element in the array?

This is an easy way to get the numbers from the end.


$urlBits = [url=http://uk.php.net/manual/en/function.explode.php]explode[/url]($url);
$last = [url=http://uk.php.net/manual/en/function.count.php]count[/url]($urlBits)-1;
$numbersAtTheEnd = $urlBits[$last];

If you need to use preg_match then you could use:


$pattern = '%^(?:http://)?(?:www\.)?example.com(?:\:80)?/(?:\w+/){4}(\d+)$%i';
if (preg_match($url, $pattern, $matches)) {
$numbersAtTheEnd = $matches[0]; // hopefully ;)
}
else {
echo 'Either the pattern is wrong or the url is...';
}

Welcome to Webmaster World :)

[edited by: PHP_Chimp at 4:49 pm (utc) on Sep. 16, 2008]

residual

6:17 pm on Sep 16, 2008 (gmt 0)

10+ Year Member



Thanks for the welcome! Unfortunately nethier one worked. Here's the code I'm trying to modify. Keep in mind this is the url
http://www.example.com/viewVideo/Flags_/Playstation_3/FRACTURE_the_weapons/886086 and it may change the directories names and the number of directories as well. I just want the numbers at the end.

if (eregi("example.com", $link3))
{
$muster= '#'.$urlex[0].'example.com/([^\.]+)(?:[^"]+)?'.$urlex[1].'#';
$resultat= preg_match($muster, $link1, $matches);
$link2= '<object width="640" height="510"><param name="movie" value="http://example.com/'. $matches[2] . '"></param><param name="wmode" value="transparent"></param><embed src=http://example.com/' . $matches[2] . '/2 type="application/x-shockwave-flash" wmode="transparent" width="640" height="510"></embed></object>';
$link1=$link2;
$site=29;
}

penders

7:53 pm on Sep 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would have thought PHP_Chimps first method should work if you just want the number at the end of the url, except you are missing the boundary delimiter from the explode() function...
$urlBits = explode('/',$url);

Or even something like:

$num = substr(strrchr($url, '/'), 1);

Cast it if you need an actual number/integer.

A regular expression seems a bit overkill if this is what you require?

PHP_Chimp

9:24 am on Sep 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops, my bad. Should have double checked my explode code.

The regular expression that I posted will look through a fixed number of directories. So although it could be modified to look through an unknown number using a regex is overkill for what you want.

I would go with explode or substr methods, as these will probably be faster than a regex.

[edited by: PHP_Chimp at 9:28 am (utc) on Sep. 17, 2008]