Page 1 of 1

Using preg_match to extract a URL

Posted: Wed Sep 11, 2002 11:01 am
by auaero
Hi guys, need a little help here.

I'm a noob with regexp, so maybe you guys can can figure this one out. I've got a string that's got a URL in it. The URL always starts with a forward slash (/) and ends with the the text "searchradius=5" (minus the quotes, of course). I want to pull the url out and store it as $URL. Here's what I tried.

Code: Select all

preg_match("/\*searchradius=5",$movie,$URL);
This returns "Warning: No ending delimiter '/' found " .

What am I doing wrong? Any help would be greatly appreciated.

Thanks,
AUaero

Posted: Wed Sep 11, 2002 11:31 am
by dusty

Code: Select all

preg_match("/\/.*searchradius=5/i",$movie,$url);
echo $urlї0];
if that's not what you're looking for, give an example input and output.

nothing returns to the $URL array

Posted: Wed Sep 11, 2002 11:50 am
by auaero
dusty wrote:

Code: Select all

preg_match("/\/.*searchradius=5/i",$movie,$url);
echo $urlї0];
if that's not what you're looking for, give an example input and output.
Thanks, Dusty. That gets rid of the error and after rereading some regexp info, I understand what's going on. However, this doesn't return anything to the array $URL. Maybe my expression is wrong. Here's an example of the URL I'm trying to capture.

/showtimes/movie_detail.asp?searchmovie=36611&searchdate=9/11/2002&searchzip=35957&searchradius=5

The URL always starts with "/" and ends with "searchradius=5". Am I using the correct expression?

Thanks,
AUaero

Posted: Wed Sep 11, 2002 11:58 am
by dusty
$url won't return anything. you need to use $url[0] to print the url that was matched.

Code: Select all

<?
$movie = "this is a test to see if /showtimes/movie_detail.asp?searchmovie=36611&searchdate=9/11/2002&searchzip=35957&searchradius=5 shows up";
preg_match("/\/.*searchradius=5/i",$movie,$url);
echo $urlї0];
?>
would print: /showtimes/movie_detail.asp?searchmovie=36611&searchdate=9/11/2002&searchzip=35957&searchradius=5

Thanks... here's another one

Posted: Wed Sep 11, 2002 1:13 pm
by auaero
Yeah, I figured out what I was doing wrong trying to display the URL. Sorry... I've only been coding PHP about a week now. : ) On a similar note, now that I've stored the URLs in an array, I want remove them from the $movie array. Here's what I'm trying:

Code: Select all

preg_replace("/\/.*searchradius=5/i"," ",$movieї$i]);
I also tried

Code: Select all

eregi_replace(/\/.*searchradius=5"," ",$movieї$i]);
No dice there either. Any idea what I'm doing wrong with this one?

Thanks again,
AUaero

Posted: Thu Sep 12, 2002 12:47 am
by dusty
$movie[$i] = eregi_replace("\/.*searchradius=5","",$movie[$i]);